백준 [2174] 로봇 시뮬레이션
문제보기
풀이
반례
Sample input
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
Output for sample input
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static class Robot {
int x;
int y;
int dir;
public Robot(int x, int y, int dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
}
static class Command {
int num;
String sort;
int cnt;
public Command(int num, String sort, int cnt) {
this.num = num;
this.sort = sort;
this.cnt = cnt;
}
}
static int A, B, N, M, ans;
static Robot[] robot;
static Command[] command;
static int[][] chk;
static int[] dx = { -1, 0, 1, 0 }; // 북서남동
static int[] dy = { 0, -1, 0, 1 };
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
ans = -1;
chk = new int[A][B];
robot = new Robot[N + 1];
for (int i = 1; i < N + 1; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken()) - 1;
int y = Integer.parseInt(st.nextToken()) - 1;
String s = st.nextToken();
int dir = -1;
if (s.equals("N")) // 북
dir = 3;
else if (s.equals("W")) // 서
dir = 0;
else if (s.equals("S")) // 남
dir = 1;
else if (s.equals("E")) // 동
dir = 2;
robot[i] = new Robot(x, y, dir);
chk[x][y] = i;
}
command = new Command[M];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
String sort = st.nextToken();
int cnt = Integer.parseInt(st.nextToken());
command[i] = new Command(num, sort, cnt);
}
int[] ans = work();
if (ans[0] == 1)
System.out.println("Robot " + ans[1] + " crashes into the wall");
else if (ans[0] == 2)
System.out.println("Robot " + ans[1] + " crashes into robot " + ans[2]);
else
System.out.println("OK");
}
public static int[] work() {
for (int i = 0; i < command.length; i++) {
Command c = command[i];
int[] result = chk(c);
if (result[0] > 0)
return result;
}
return new int[] { 0, 0, 0 };
}
// return 0:통과 , 1:벽, 2:로봇
public static int[] chk(Command c) {
Robot r = robot[c.num];
int[] result = new int[3]; // 결과, X,Y
switch (c.sort) {
case "L":
int turnL = c.cnt % 4;
int dirL = r.dir;
turnL += dirL;
if (turnL >= 4)
turnL %= 4;
robot[c.num].dir = turnL;
break;
case "R":
int turnR = c.cnt % 4;
int dirR = r.dir;
turnR = 4 - (turnR - dirR);
turnR %= 4;
robot[c.num].dir = turnR;
break;
case "F":
int x = r.x;
int y = r.y;
for (int i = 1; i < c.cnt + 1; i++) {
x += dx[r.dir];
y += dy[r.dir];
if (isRange(x, y)) {
if (chk[x][y] != 0) {
result[0] = 2;
result[1] = c.num;
result[2] = chk[x][y];
return result;
}
} else {
result[0] = 1;
result[1] = c.num;
return result;
}
}
chk[r.x][r.y] = 0;
chk[x][y] = c.num;
robot[c.num].x = x;
robot[c.num].y = y;
break;
}
return result;
}
public static boolean isRange(int x, int y) {
return x >= 0 && y >= 0 && x < A && y < B;
}
}