나의 공부장

백준 2174 로봇 시뮬레이션 [Simulation] 본문

알고리즘/BOJ

백준 2174 로봇 시뮬레이션 [Simulation]

꾸준한나 2020. 5. 8. 18:51

문제 링크: https://www.acmicpc.net/problem/2174

 

2174번: 로봇 시뮬레이션

문제 가로 A(1≤A≤100), 세로 B(1≤B≤100) 크기의 땅이 있다. 이 땅 위에 로봇들이 N(1≤N≤100)개 있다. 로봇들의 초기 위치는 x좌표와 y좌표로 나타난다. 위의 그림에서 보듯 x좌표는 왼쪽부터, y좌표

www.acmicpc.net

문제 풀이

기존에 풀던 (x, y) 좌표와는 살짝 달라서 헷갈릴 수 있지만, 문제의 설명대로 따라서 구현하면 됩니다.

x좌표는 왼쪽에서 오른쪽으로 가면 증가하고

y좌표는 아래에서 위로 올라가면 증가합니다.

 

로봇의 상태를 구조체로 만들어서 벡터로 보관해서,

그 이후에 명령을 하나씩 실행하면서 그에 따른 구현을 해주면 됩니다.

 

따로 visited라는 2차원 배열 pair를 만들었는데, 이 부분은 해당 로봇이 명령에 의해서 움직인 공간에 이미 다른 로봇이 있는 경우를 체크해주기 위해서 구현했습니다.

 

사실 다 구현해놓고 보니까 코드가 지저분하네요. 반복되는 부분을 함수화하는 능력이 아직 부족한 것 같습니다.

 

소스 코드
#include <iostream>
#include <vector>
using namespace std;

typedef struct
{
	int x, y;
	char direction;
}Robot;

int A, B, N, M;                    // 변수
int a[101][101];                   // 맵
pair<int, bool> visited[101][101]; // (로봇 번호, 방문 여부)
vector<Robot> robots(101);         // 로봇상태
bool ok = true;                    // 시뮬레이션 검증

void show(Robot& now)
{
	cout << "[" << now.x << "," << now.y << "]" << ' ' << now.direction << endl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	cin >> A >> B >> N >> M;

	// 초기
	for (int i = 1; i <= N; i++)
	{
		cin >> robots[i].x >> robots[i].y >> robots[i].direction;
		visited[robots[i].x][robots[i].y].first = i;
		visited[robots[i].x][robots[i].y].second = true;
	}

	// 명령
	for (int i = 0; i < M; i++)
	{
		int robot_num, loop;
		char command;
		cin >> robot_num >> command >> loop;

		Robot& now = robots[robot_num]; // 현재 로봇
		int nx, ny;                     // 명령이 수행되었을 때의 좌표

		// 명령어는 총 F, R, L
		if (command == 'F')
		{
			if (now.direction == 'N')
			{
				for (int j = 0; j < loop; j++)
				{
					nx = now.x; ny = now.y + 1;

					// 다른 로봇이 있는지 체크
					if (visited[nx][ny].second == true)
					{
						cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
						ok = false;
						break;
					}

					// 경계 체크
					if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
					{
						visited[now.x][now.y].second = false;
						visited[nx][ny].second = true;
						now.x = nx;
						now.y = ny;
					}
					else
					{
						cout << "Robot " << robot_num << " crashes into the wall" << endl;
						ok = false;
						break;
					}
				}
			}
			else if (now.direction == 'E')
			{
				for (int j = 0; j < loop; j++)
				{
					nx = now.x + 1; ny = now.y;

					if (visited[nx][ny].second == true)
					{
						cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
						ok = false;
						break;
					}

					if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
					{
						visited[now.x][now.y].second = false;
						visited[nx][ny].second = true;
						now.x = nx;
						now.y = ny;
					}
					else
					{
						cout << "Robot " << robot_num << " crashes into the wall" << endl;
						ok = false;
						break;
					}
				}
			}
			else if (now.direction == 'W')
			{
				for (int j = 0; j < loop; j++)
				{
					nx = now.x - 1; ny = now.y;
					
					if (visited[nx][ny].second == true)
					{
						cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
						ok = false;
						break;
					}

					if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
					{
						visited[now.x][now.y].second = false;
						visited[nx][ny].second = true;
						now.x = nx;
						now.y = ny;
					}
					else
					{
						cout << "Robot " << robot_num << " crashes into the wall" << endl;
						ok = false;
						break;
					}
				}
			}
			else if (now.direction == 'S')
			{
				for (int j = 0; j < loop; j++)
				{
					nx = now.x; ny = now.y - 1;

					if (visited[nx][ny].second == true)
					{
						cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
						ok = false;
						break;
					}

					if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
					{
						visited[now.x][now.y].second = false;
						visited[nx][ny].second = true;
						now.x = nx;
						now.y = ny;
					}
					else
					{
						cout << "Robot " << robot_num << " crashes into the wall" << endl;
						ok = false;
						break;
					}
				}
			}

			// 충돌발생!
			if (ok == false) break;
		}
		else if (command == 'R')
		{
			for (int j = 0; j < loop; j++)
			{
				if (now.direction == 'N') now.direction = 'E';
				else if (now.direction == 'S') now.direction = 'W';
				else if (now.direction == 'W') now.direction = 'N';
				else if (now.direction == 'E') now.direction = 'S';
			}
		}
		else if (command == 'L')
		{
			for (int j = 0; j < loop; j++)
			{
				if (now.direction == 'N') now.direction = 'W';
				else if (now.direction == 'S') now.direction = 'E';
				else if (now.direction == 'W') now.direction = 'S';
				else if (now.direction == 'E') now.direction = 'N';
			}
		}
	}

	if (ok) cout << "OK" << endl;


	return 0;
}