백준알고리즘

로봇 시뮬레이션

먼지의삶 2019. 10. 9. 03:33

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

 

2174번: 로봇 시뮬레이션

문제 가로 A(1≤A≤100), 세로 B(1≤B≤100) 크기의 땅이 있다. 이 땅 위에 로봇들이 N(1≤N≤100)개 있다. 로봇들의 초기 위치는 x좌표와 y좌표로 나타난다. 위의 그림에서 보듯 x좌표는 왼쪽부터, y좌표는 아래쪽부터 순서가 매겨진다. 또한 각 로봇은 맨 처음에 NWES 중 하나의 방향을 향해 서 있다. 초기에 서 있는 로봇들의 위치는 서로 다르다. 이러한 로봇들에 M(1≤M≤100)개의 명령을 내리려고 한다. 각각의 명령은 순차적으로

www.acmicpc.net

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
#include<iostream>
#include<vector>
#include<string>
 
using namespace std;
 
int a, b, n, m;
struct robot {
    int x, y,dir;
    robot(int x, int y,int dir) {
        this->= x;
        this->= y;
        this->dir = dir;
    }
};
vector<robot> rb;
struct cmd {
    int num;
    char com;
    int rep;
    cmd(int num, char com, int rep) {
        this->num = num;
        this->com = com;
        this->rep = rep;
    }
};
vector<cmd> cd;
vector<string>er;
int dx[] = { 0,-1,0,1 };
int dy[] = { 1,0,-1,0 };
int map[100][100];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> a >> b >> n >> m;//a가로, b세로;;
    for (int i = 0; i < n; i++) {
        int x, y; char dir;
        cin >> x >> y >> dir;
        switch (dir) {
        case 'N':
            rb.push_back(robot(y, x, 3));
            map[y][x] += 1;
            break;
        case 'S':
            rb.push_back(robot(y, x, 1));
            map[y][x] += 1;
            break;
        case 'E':
            rb.push_back(robot(y, x, 0));
            map[y][x] += 1;
            break;
        case 'W':
            rb.push_back(robot(y, x, 2));
            map[y][x] += 1;
            break;
        }
    }
    for (int i = 0; i < m; i++) {
        int num, rp; char cm;
        cin >> num >> cm >> rp;
        cd.push_back(cmd(num - 1, cm, rp));
    }
    for (int i = 0; i < cd.size(); i++) {
        int idx = cd[i].num;
        int x = rb[idx].x; int y = rb[idx].y;
        int dir = rb[idx].dir;
 
        for (int j = 1; j <= cd[i].rep; j++) {
            //0 동 1 남 2 서 3북
            if (cd[i].com == 'L') {
                rb[idx].dir-=1;
                if (rb[idx].dir < 0) rb[idx].dir = 3;
            }
            else if (cd[i].com == 'R') {
                rb[idx].dir+=1;
                if (rb[idx].dir == 4) rb[idx].dir = 0;
            }
            else if (cd[i].com == 'F') {
                rb[idx].x += dx[rb[idx].dir]; rb[idx].y += dy[rb[idx].dir];
                if (rb[idx].x <= 0 || rb[idx].x > b || rb[idx].y <= 0 || rb[idx].y > a) {
                    string sho = "Robot ";
                    string xo = to_string(idx + 1);
                    sho += xo;
                    sho += " crashes into the wall";
                    cout << sho << '\n';
                    return 0;
                }
                map[x][y] -= 1;
                map[rb[idx].x][rb[idx].y] += 1;
                if (map[rb[idx].x][rb[idx].y] > 1) {
                    for (int k = 0; k < rb.size(); k++) {
                        if (idx == k) continue;
                        if (rb[idx].x == rb[k].x && rb[idx].y == rb[k].y) {
                            string sho = "Robot ";
                            string x1 = to_string(idx + 1);
                            sho += x1;
                            sho += " crashes into robot ";
                            x1 = to_string(k+1);
                            sho += x1;
                            cout << sho << '\n';
                            return 0;
                        }
                    }
                }
            }
        }
    }
    if (er.size() == 0)
        cout << "OK" << '\n';
    
    return 0;
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

시뮬레이션 문제, 그냥 문제그대로만따라가면 어렵진않으나 역시 함정이 하나있다.

문제를 아무리읽어봐도 에러 하나나면 그대로 끝나는지에 대한 언급이없어서 처음에 에러를 에러 벡터에 담아서 출력하는걸생각했다.

에러 하나 나면, 그대로 끝내면 문제를해결할수있다.