백준 2174번 로봇 시뮬레이션 문제풀이(C++)

YooHeeJoon·2023년 1월 25일
0

백준 문제풀이

목록 보기
47/56

백준 2174번 로봇 시뮬레이션

아이디어

로봇이 가지고 있는 정보는 x좌표, y좌표, 바라보고 있는 방향 3가지 ==> 구조체로 구현하자

struct robot
{
	int x; // x좌표
	int y; // y좌표
	int side; // 바라보고 있는 방향
};

내릴 수 있는 명령 3개

  1. L: 로봇이 향하고 있는 방향을 기준으로 왼쪽으로 90도 회전한다.
if (order == 'L')
{
	robots[robot_num].side = (robots[robot_num].side + 4 - repeat_count % 4) % 4;
}
  1. R: 로봇이 향하고 있는 방향을 기준으로 오른쪽으로 90도 회전한다.
if (order == 'R')
{
	robots[robot_num].side = (robots[robot_num].side + repeat_count % 4) % 4;
}
  1. F: 로봇이 향하고 있는 방향을 기준으로 앞으로 한 칸 움직인다.
if(order == 'F')
{
	robots[robot_num].x += dx[robots[robot_num].side];
	robots[robot_num].y += dy[robots[robot_num].side];
}

잘못된 명령 2개

// 해당 로봇이 이동할 좌표 nx, ny
const int nx = robots[robot_num].x + dx[robots[robot_num].side];
const int ny = robots[robot_num].y + dy[robots[robot_num].side];
  1. X번 로봇이 벽에 충돌하는 경우
if (nx < 1 || nx > a || ny < 1 || ny > b)
{
	return "Robot " + to_string(robot_num) + " crashes into the wall";
}
  1. X번 로봇이 움직이다가 Y번 로봇에 충돌하는 경우
if (ground[nx][ny]) // 로봇의 위치 정보를 가지고 있는 ground 배열
{
	return "Robot " + to_string(robot_num) + " crashes into robot " + to_string(ground[nx][ny]);
}

정답 코드

#include<bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
struct robot
{
	int x;
	int y;
	int side;
};
constexpr int dx[4] = { 0,1,0,-1 };
constexpr int dy[4] = { 1,0,-1,0 };
string move(vector<vector<int>>& ground, vector<robot>& robots, const int a, const int b, const int robot_num, const char order, int repeat_count)
{
	if (order == 'F')
	{
		while (repeat_count--)
		{
			const int nx = robots[robot_num].x + dx[robots[robot_num].side];
			const int ny = robots[robot_num].y + dy[robots[robot_num].side];
			if (nx < 1 || nx > a || ny < 1 || ny > b)
			{
				return "Robot " + to_string(robot_num) + " crashes into the wall";
			}
			if (ground[nx][ny])
			{
				return "Robot " + to_string(robot_num) + " crashes into robot " + to_string(ground[nx][ny]);
			}
			ground[nx][ny] = robot_num;
			ground[robots[robot_num].x][robots[robot_num].y] = 0;
			robots[robot_num].x += dx[robots[robot_num].side];
			robots[robot_num].y += dy[robots[robot_num].side];
		}
	}
	else if (order == 'L')
	{
		robots[robot_num].side = (robots[robot_num].side + 4 - repeat_count % 4) % 4;
	}
	else if (order == 'R')
	{
		robots[robot_num].side = (robots[robot_num].side + repeat_count % 4) % 4;
	}
	return "OK";
}
int main() {
	FAST_IO;
	int a, b; cin >> a >> b;
	int n, m; cin >> n >> m;
	vector<robot> robots(n + 1);
	vector<vector<int>> ground(a + 1, vector<int>(b + 1));
	for (int i = 1; i <= n; i++)
	{
		char side;
		cin >> robots[i].x >> robots[i].y >> side;
		if (side == 'N') robots[i].side = 0;
		else if (side == 'E') robots[i].side = 1;
		else if (side == 'S') robots[i].side = 2;
		else robots[i].side = 3;
		ground[robots[i].x][robots[i].y] = i;
	}
	string error_msg = "OK";
	while (m--)
	{
		int robot_num, repeat_count;
		char order;
		cin >> robot_num >> order >> repeat_count;
		if (error_msg == "OK")
		{
			error_msg = move(ground, robots, a, b, robot_num, order, repeat_count);
		}
	}
	cout << error_msg << '\n';
	return 0;
}

0개의 댓글