1. 문제 링크
2174번: 로봇 시뮬레이션
첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순
www.acmicpc.net
2. 문제 및 입출력예제
+) 도움 받았던 테케들입니다. 반례 모으기 힘들었어서 모아봤어요
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
Robot 1 crashes into the wall
1 3
2 1
1 1 N
1 2 N
1 F 2
Robot 1 crashes into robot 2
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
Robot 1 crashes into robot 2
3 3
1 9
2 2 W
1 F 1
1 L 1
1 F 1
1 L 1
1 F 2
1 L 5
1 F 2
1 R 3
1 F 2
OK
5 5
9 1
3 1 W
3 2 N
3 4 S
3 5 E
1 3 S
2 3 N
4 3 E
5 4 W
3 3 W
9 F 4
Robot 9 crashes into robot 6
5 5
9 1
3 1 W
3 2 N
3 4 S
3 5 E
1 3 S
2 3 N
4 3 E
5 4 W
3 3 S
9 F 4
Robot 9 crashes into robot 2
5 5
9 1
3 1 W
3 2 N
3 4 S
3 5 E
1 3 S
2 3 N
4 3 E
5 4 W
3 3 N
9 F 4
Robot 9 crashes into robot 3
5 5
9 1
3 1 W
3 2 N
3 4 S
3 5 E
1 3 S
2 3 N
4 3 E
5 4 W
3 3 E
9 F 4
Robot 9 crashes into robot 7
5 5
2 3
3 3 E
4 5 N
2 L 3
2 R 8
2 F 3
Robot 2 crashes into the wall
46 46
9 100
39 25 E
15 14 N
15 22 N
27 25 S
25 40 S
19 36 W
27 6 W
22 45 W
28 15 W
5 F 13
3 R 2
8 F 9
5 L 4
6 F 7
3 F 12
9 F 5
6 L 2
3 L 2
3 F 13
2 L 2
4 F 12
9 L 2
9 F 2
5 F 1
3 F 3
9 L 2
3 L 5
3 L 21
3 F 6
4 R 2
4 F 1
6 F 13
9 R 2
9 R 29
6 L 2
2 L 9
2 F 13
4 L 2
1 F 14
5 F 1
6 F 1
8 L 2
6 F 1
3 R 13
4 F 4
3 F 6
6 F 4
2 L 2
5 F 1
9 R 14
3 R 2
6 R 2
9 L 12
3 F 3
7 F 1
9 L 2
6 F 5
6 F 12
4 F 5
3 R 2
6 R 29
6 F 10
9 F 1
2 F 5
9 F 10
4 F 4
4 R 31
9 L 8
3 L 2
4 R 27
3 F 4
1 F 1
1 R 3
5 L 10
6 R 17
7 F 15
8 F 8
9 F 7
6 F 11
8 L 3
2 F 3
1 L 2
3 L 3
4 R 2
7 L 11
1 L 30
8 L 13
9 F 11
2 L 2
8 R 2
3 R 2
3 R 2
8 L 2
4 L 2
2 F 8
8 F 14
6 R 6
9 F 13
2 F 11
5 F 7
1 F 14
1 R 7
9 L 28
3 L 4
6 F 14
7 F 5
2 R 29
5 F 1
8 L 4
Robot 3 crashes into robot 2
3. 문제 풀이
- 입력 받을 때 좌표를 0, 0 부터 시작하기 위해 1을 뺀 좌표로 입력받았다.
- 로롯 배열에 입력받은 값을 넣고, map 좌표에 해당 로봇 번호를 넣었다.
- 명령어에 따라서 입력받은 로봇의 위치를 찾고 명령어에 따라 동작한다.
- 명령어가 끝날 때 변경된 정보로 업데이트한다.
4. 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static class Robot {
int x;
int y;
int dir;
public Robot(int x, int y, int dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
}
}
static Robot[] robots;
static int dy[] = { 1, 0, -1, 0 };
static int dx[] = { 0, 1, 0, -1 };
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
int A = Integer.parseInt(split[0]);
int B = Integer.parseInt(split[1]);
int[][] map = new int[B][A];
split = br.readLine().split(" ");
int N = Integer.parseInt(split[0]);
int M = Integer.parseInt(split[1]);
robots = new Robot[N + 1];
int x, y, dir = 0;
for (int i = 1; i < N + 1; i++) {
split = br.readLine().split(" ");
x = Integer.parseInt(split[0]) - 1;
y = Integer.parseInt(split[1]) - 1;
if (split[2].equals("N")) {
dir = 0;
} else if (split[2].equals("E")) {
dir = 1;
} else if (split[2].equals("S")) {
dir = 2;
} else if (split[2].equals("W")) {
dir = 3;
}
robots[i] = new Robot(x, y, dir);
map[y][x] = i;
}
String nfr;
int num, robot, X = 0, Y = 0;
boolean flag = true;
out:
for (int i = 0; i < M; i++) {
split = br.readLine().split(" ");
robot = Integer.parseInt(split[0]);
nfr = split[1];
num = Integer.parseInt(split[2]);
for (int j = 0; j < num; j++) {
Robot now = robots[robot];
dir = now.dir;
if (nfr.equals("L")) {
dir--;
if (dir == -1) {
dir = 3;
}
robots[robot] = new Robot(now.x, now.y, dir);
} else if (nfr.equals("R")) {
dir++;
if (dir == 4) {
dir = 0;
}
robots[robot] = new Robot(now.x, now.y, dir);
} else if (nfr.equals("F")) {
X = now.x + dx[now.dir];
Y = now.y + dy[now.dir];
if (X < 0 || X >= A || Y < 0 || Y >= B) {
System.out.print("Robot " + (robot) + " crashes into the wall");
flag = false;
break out;
} else if (map[Y][X] != 0) {
System.out.print("Robot " + (robot) + " crashes into robot " + (map[Y][X]));
flag = false;
break out;
}
map[now.y][now.x] = 0;
map[Y][X] = robot;
robots[robot] = new Robot(X, Y, now.dir);
}
}
}
if (flag) {
System.out.print("OK");
}
}
}
'ALGORITHM' 카테고리의 다른 글
백준 11559 Puyo Puyo (Java) (0) | 2023.04.14 |
---|---|
백준 10816 숫자 카드 2 (Java) (0) | 2023.04.14 |
백준 1978 소수 찾기 (Java) (0) | 2023.04.13 |
백준 10845 큐 (Java) (1) | 2023.04.12 |
백준 3028 창영마을 (Java) (0) | 2023.04.12 |