코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
static int N, M, A, B;
static Map<String, Direction> directions = new HashMap<>();
static String[] robotsDirection;
static int[][] robots, map;
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());
robots = new int[N + 1][2];
map = new int[B + 1][A + 1];
robotsDirection = new String[N + 1];
initialize();
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
robots[i][0] = y;
robots[i][1] = x;
robotsDirection[i] = st.nextToken();
map[y][x] = i;
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int robotNum = Integer.parseInt(st.nextToken());
String command = st.nextToken();
int commandCount = Integer.parseInt(st.nextToken());
for (int count = 0; count < commandCount; count++) {
if (!executeCommand(robotNum, command)) return;
}
}
System.out.println("OK");
}
private static void initialize() {
directions.put("E", new Direction("N", "S", new int[]{0, 1}));
directions.put("S", new Direction("E", "W", new int[]{-1, 0}));
directions.put("N", new Direction("W", "E", new int[]{1, 0}));
directions.put("W", new Direction("S", "N", new int[]{0, -1}));
}
private static boolean executeCommand(int robotNum, String command) {
if (command.equals("F")) return goForward(robotNum);
else if (command.equals("L")) return turnLeft(robotNum);
else return turnRight(robotNum);
}
private static boolean goForward(int robotNum) {
String currentDirection = robotsDirection[robotNum];
Direction info = directions.get(currentDirection);
int[] diff = info.F;
int ny = diff[0] + robots[robotNum][0];
int nx = diff[1] + robots[robotNum][1];
if (ny > B || ny <= 0 || nx > A || nx <= 0) {
System.out.println("Robot " + robotNum + " crashes into the wall");
return false;
}
if (map[ny][nx] != 0) {
System.out.println("Robot " + robotNum + " crashes into robot " + map[ny][nx]);
return false;
}
robots[robotNum][0] = ny;
robots[robotNum][1] = nx;
map[ny][nx] = robotNum;
map[ny - diff[0]][nx - diff[1]] = 0;
return true;
}
private static boolean turnLeft(int robotNum) {
String currentDirection = robotsDirection[robotNum];
Direction info = directions.get(currentDirection);
robotsDirection[robotNum] = info.L;
return true;
}
private static boolean turnRight(int robotNum) {
String currentDirection = robotsDirection[robotNum];
Direction info = directions.get(currentDirection);
robotsDirection[robotNum] = info.R;
return true;
}
static class Direction {
String L, R;
int[] F;
public Direction(String l, String r, int[] f) {
L = l;
R = r;
F = f;
}
}
}
- 구현문제 풀때는 항상 엣지케이스 테스트 잘하기