๐Ÿ˜‰/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฑ€ (๋ฐฑ์ค€, 3190)

ruhz 2020. 11. 20. 15:43

๋ฌธ์ œ

 

3190๋ฒˆ: ๋ฑ€

 'Dummy' ๋ผ๋Š” ๋„์Šค๊ฒŒ์ž„์ด ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์—๋Š” ๋ฑ€์ด ๋‚˜์™€์„œ ๊ธฐ์–ด๋‹ค๋‹ˆ๋Š”๋ฐ, ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ฉด ๋ฑ€ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค. ๋ฑ€์ด ์ด๋ฆฌ์ €๋ฆฌ ๊ธฐ์–ด๋‹ค๋‹ˆ๋‹ค๊ฐ€ ๋ฒฝ ๋˜๋Š” ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค. ๊ฒŒ์ž„

www.acmicpc.net

 

 

ํ’€์ด

 

ruhz3/CodingTest

To prepare for coding test. Contribute to ruhz3/CodingTest development by creating an account on GitHub.

github.com

#include <iostream>
#include <utility>
#include <queue>
using namespace std;

int N;
int appleNum;
int rotateNum;
int map[101][101];

vector<pair<int, int>> Snake;
queue<pair<int, int>> Rotate;
int currentTime = 1;
int currentDir = 2;

// ํ•ด๋‹น ์‹œ๊ฐ„์— ๋ฐฉํ–ฅ์„ ์ „ํ™˜
int changeDirection(int time, int dir) {
	if (Rotate.front().first == time) {
		dir += Rotate.front().second;
		if (dir == 5)
			dir = 1;
		if (dir == 0)
			dir = 4;
		Rotate.pop();
	}
	return dir;
}

// ๋ฑ€์„ ๊ทœ์น™์— ๋งž๊ฒŒ ์ด๋™
bool moveSnake() {
	int current_head_I = Snake.back().first;
	int current_head_J = Snake.back().second;

	// ๋‹ค์Œ head์˜ ์ขŒํ‘œ ์ฐพ๊ธฐ
	int next_head_I;
	int next_head_J;

	switch (currentDir) {
	case 1: // ์ƒ
		next_head_I = -1;
		next_head_J = 0;
		break;
	case 2: // ์šฐ
		next_head_I = 0;
		next_head_J = 1;
		break;
	case 3: // ํ•˜
		next_head_I = 1;
		next_head_J = 0;
		break;
	case 4: // ์ขŒ
		next_head_I = 0;
		next_head_J = -1;
		break;
	}
	next_head_I += current_head_I;
	next_head_J += current_head_J;

	// head์˜ ๋ฒฝ ์ถฉ๋Œ ๊ฒ€์‚ฌ
	if (next_head_I > N || next_head_J > N || next_head_I < 1 || next_head_J < 1) {
		return false;
	}
	// head์˜ ์ž๊ธฐ์ž์‹  ์ถฉ๋Œ ๊ฒ€์‚ฌ
	for (int i = 0; i < Snake.size() - 1; i++) {
		if (Snake[i].first == next_head_I && Snake[i].second == next_head_J) {
			return false;
		}
	}
	// head๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ 
	Snake.push_back(make_pair(next_head_I, next_head_J));

	// head์œ„์น˜์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด ๊ผฌ๋ฆฌ๋ฅผ ์‚ญ์ œ
	if (map[next_head_I][next_head_J] != -1) {
		Snake.erase(Snake.begin());
	}
	// head์œ„์น˜์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ์‚ฌ๊ณผ๋ฅผ ์‚ญ์ œ
	else {
		map[next_head_I][next_head_J] = 0;
	}
	return true;
}

int main() {
	cin >> N;
	
	// ์‚ฌ๊ณผ ์œ„์น˜ ์ž…๋ ฅ
	int getI;
	int getJ;
	cin >> appleNum;
	for (int i = 0; i < appleNum; i++) {
		cin >> getI >> getJ;
		map[getI][getJ] = -1;
	}
	
	// ํšŒ์ „ ์ •๋ณด ์ž…๋ ฅ
	int time;
	char Dir;
	pair<int, int> input;
	cin >> rotateNum;
	for (int i = 0; i < rotateNum; i++) {
		cin >> time >> Dir;
		input.first = time;

		// ์˜ค๋ฅธ์ชฝ์€ ์‹œ๊ณ„๋ฐฉํ–ฅ(+1), ์™ผ์ชฝ์€ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ(-1)
		if (Dir == 'L') input.second = -1;
		else if (Dir == 'D') input.second = 1;

		Rotate.push(input);
	}

	Snake.push_back(make_pair(1, 1));

	// moveSnake๊ฐ€ ์ข…๋ฃŒ์กฐ๊ฑด์— ์•ˆ ๊ฑธ๋ฆฌ๋ฉด ๊ณ„์† ๋ฑ€ ์ด๋™
	while (moveSnake()) {
		if (Rotate.size() > 0) {
			currentDir = changeDirection(currentTime, currentDir);
		}
		currentTime++;
	}

	cout << currentTime << endl;

	return 0;
}

๋ฑ€์ด ๊ทœ์น™์— ๋งž๊ฒŒ ์›€์ง์ด๋Š” ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ ์กฐ๊ฑด์— ๊ฑธ๋ ธ๋Š”์ง€ ์•ˆ ๊ฑธ๋ ธ๋Š”์ง€๋ฅผ bool๊ฐ’์„ returnํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ๋‹ค.
๋ฐฉํ–ฅ์€ 1, 2, 3, 4๋ฅผ ์ƒ, ์šฐ, ํ•˜, ์ขŒ(์‹œ๊ณ„๋ฐฉํ–ฅ) ์ˆœ์„œ๋Œ€๋กœ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น ์‹œ๊ฐ„์— ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ํ–ˆ๋‹ค.

โ€‹๋‘๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ while๋ฌธ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ฉ์ณค๊ณ , ํ•œ ๋ฒˆ loop์„ ๋Œ ๋•Œ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ์ฆ๊ฐ€์‹œ์ผœ์คฌ๋‹ค.

โ€‹ํŒ€์›๊ณผ ๋ฐฉํ–ฅ์„ ๋‹ค๋ฅด๊ฒŒ ์งฐ์œผ๋‚˜, ์„œ๋กœ ์ง  ์ฝ”๋“œ๋ฅผ ๊ธˆ๋ฐฉ ์ดํ•ดํ•˜๊ณ  ๋„์›€์„ ์ค˜์„œ ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•˜๊ฒŒ ์งœ์—ฌ์ง„ ๊ฒƒ ๊ฐ™์•„ ์ข‹์•˜๋‹ค.