본문 바로가기
알고리즘/백준 - 골드

백준 2174 로봇 시뮬레이션

by 호롤롤로루야 2021. 9. 7.

백준 2174 로봇 시뮬레이션

1. 문제 링크

https://www.acmicpc.net/problem/2174

 

2174번: 로봇 시뮬레이션

첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순

www.acmicpc.net

 

2. 문제 해결에 대한 아이디어

1. input의 2차원 배열 인덱스가 좌측 하단부터 시작되므로, 익숙한 형태로 보정해야 한다.

2. 명령 정보를 담을 Command 클래스, 로봇 정보를 담을 Robot 클래스가 필요하다.

3. Robot 타입의 2차원 배열을 선언하고, 그 위에서 로봇을 움직인다.

4. 나머지는 문제에서 요구하는대로 구현하면 된다.

 

3. 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {
	static int A, B, N, M;
	static Robot[][] board;

	static int[] dirY = { -1, 0, 1, 0 };
	static int[] dirX = { 0, 1, 0, -1 };
	static List<Robot> robots = new ArrayList<>();
	static Queue<Robot> q = new LinkedList<>();
	static List<Command> commands = new ArrayList<>();

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] ab = br.readLine().split(" ");
		A = Integer.parseInt(ab[0]);
		B = Integer.parseInt(ab[1]);

		String[] nm = br.readLine().split(" ");
		N = Integer.parseInt(nm[0]); // 세로
		M = Integer.parseInt(nm[1]); // 가로

		board = new Robot[B][A];

		for (int i = 0; i < N; i++) {
			String[] yxd = br.readLine().split(" ");
			int y = B - Integer.parseInt(yxd[1]); // 일반적인 2차원 배열의 좌표로 고치기
			int x = Integer.parseInt(yxd[0]) - 1; // 일반적인 2차원 배열의 좌표로 고치기
			String d = yxd[2];
			robots.add(new Robot(i + 1, y, x, d));
			board[y][x] = robots.get(i); // 보드에 로봇 설정
			
		}

		for (int i = 0; i < M; i++) {
			String[] cmd = br.readLine().split(" ");
			int index = Integer.parseInt(cmd[0]) - 1;
			String dir = cmd[1];
			int loop = Integer.parseInt(cmd[2]);
			commands.add(new Command(index, dir, loop));
		}

		for (int i = 0; i < commands.size(); i++) {
			Command now = commands.get(i);
			Robot robot = robots.get(now.index);

			for (int j = 0; j < now.loop; j++) {
				int y = robot.y;
				int x = robot.x;
				String d = robot.d;

				int yy, xx;
				String dd;
				if (now.cmd.equals("F")) {
					yy = robot.y + dirY[dirToInt(d)];
					xx = robot.x + dirX[dirToInt(d)];
					dd = d;
					if (yy < 0 || xx < 0 || yy >= B || xx >= A) {
						System.out.printf("Robot %d crashes into the wall\n", robot.n);
						return;
					} else if (board[yy][xx] != null) {
						System.out.printf("Robot %d crashes into robot %d\n", robot.n, board[yy][xx].n);
						return;
					}
					robot.y = yy;
					robot.x = xx;
					robot.d = dd;
					board[yy][xx] = robot;
					board[y][x] = null;
				} else if (now.cmd.equals("L")) {
					yy = robot.y;
					xx = robot.x;
					dd = turnLeft(d);

					robot.y = yy;
					robot.x = xx;
					robot.d = dd;
					board[yy][xx] = robot;
				} else { // R
					yy = robot.y;
					xx = robot.x;
					dd = turnRight(d);

					robot.y = yy;
					robot.x = xx;
					robot.d = dd;
					board[yy][xx] = robot;
				}
				
			}
		}
		System.out.println("OK");
		br.close();

	}

	static int dirToInt(String dir) {
		if (dir.equals("N")) {
			return 0;
		} else if (dir.equals("E")) {
			return 1;
		} else if (dir.equals("S")) {
			return 2;
		} else {
			return 3;
		}
	}

	static String turnLeft(String dir) {
		if (dir.equals("N")) {
			return "W";
		} else if (dir.equals("E")) {
			return "N";
		} else if (dir.equals("S")) {
			return "E";
		} else {
			return "S";
		}
	}

	static String turnRight(String dir) {
		if (dir.equals("N")) {
			return "E";
		} else if (dir.equals("E")) {
			return "S";
		} else if (dir.equals("S")) {
			return "W";
		} else {
			return "N";
		}
	}
}

class Command {
	int index;
	String cmd;
	int loop;

	public Command(int index, String cmd, int loop) {
		super();
		this.index = index;
		this.cmd = cmd;
		this.loop = loop;
	}

}

class Robot {
	int n, y, x;
	String d;

	public Robot(int n, int y, int x, String d) {
		super();
		this.n = n;
		this.y = y;
		this.x = x;
		this.d = d;
	}

}
 

4. 채점 결과

 

5. 느낀 점

1. 로봇이 왼쪽, 오른쪽으로 돌 수 있기 때문에 각각 회전 메서드를 구현했다. 귀찮았다.

2. input 배열의 위치보정 방법을 생각할 때 번거로웠다.

 

'알고리즘 > 백준 - 골드' 카테고리의 다른 글

백준 2573 빙산  (0) 2021.09.05
백준 4179 불!  (0) 2021.09.03
백준 2589 보물섬  (0) 2021.09.02

댓글