티스토리 뷰

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


"하라는 대로~~"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
 
    static int A,B,N,M;
    static int[][] map;
    static Robot[] robots;
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,1,0,-1};
    static boolean fail;
    
    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());
        
        map = new int[B][A];
        
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        
        robots = new Robot[N];
        int idx = 0;
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int d = getDir(st.nextToken().charAt(0));
            
            robots[idx++= new Robot(B-y, x-1, d);
            map[B-y][x-1= idx;
        }
        
        loop : for (int m = 0; m < M; m++) {
            st = new StringTokenizer(br.readLine());
            int robot = Integer.parseInt(st.nextToken());
            char cmd = st.nextToken().charAt(0);
            int loop = Integer.parseInt(st.nextToken());
            
            for (int i = 0; i < loop; i++) {
                if(cmd == 'L') {
                    left(robot);
                }
                else if (cmd == 'R') {
                    right(robot);
                }
                else {    // move
                    if(!go(robot)) {
                        break loop;
                    }
                }
            }
        }
        if(!fail)
            System.out.println("OK");
    }
    
    static void left (int robot) {
        Robot r = robots[robot-1];
        r.d  = (r.d - 1 == -1 ? 3 : r.d - 1);
    }
    
    static void right (int robot) {
        Robot r = robots[robot-1];
        r.d  = (r.d + 1 == 4 ? 0 : r.d + 1);
    }
    
    static boolean go (int robot) {
        int d = robots[robot-1].d;
        int x = robots[robot-1].x;
        int y = robots[robot-1].y;
        int nx = x + dx[d];
        int ny = y + dy[d];
        
        if(nx < 0 || nx >= B || ny < 0 || ny >= A) {
            System.out.printf("Robot %d crashes into the wall%n",robot);
            fail = true;
            return false;
        }
        
        if(map[nx][ny] != 0) {
            System.out.printf("Robot %d crashes into robot %d%n",robot,map[nx][ny]);
            fail = true;
            return false;
        }
        
        map[x][y] = 0;
        map[nx][ny] = robot;
        robots[robot-1].x = nx;
        robots[robot-1].y = ny;
        
        return true;
    }
    
    static int getDir(char d) {
        int temp = 0;
        switch(d) {
        case 'E': temp = 1break;
        case 'W': temp = 3break;
        case 'S': temp = 2break;
        case 'N': temp = 0break;
        }
        return temp;
    }
}
 
class Robot {
    int x;
    int y;
    int d;
    
    Robot(int x, int y, int d) {
        this.x = x;
        this.y = y;
        this.d = d;
    }
}
cs






댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday