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

 

2174번: 로봇 시뮬레이션

문제 가로 A(1≤A≤100), 세로 B(1≤B≤100) 크기의 땅이 있다. 이 땅 위에 로봇들이 N(1≤N≤100)개 있다. 로봇들의 초기 위치는 x좌표와 y좌표로 나타난다. 위의 그림에서 보듯 x좌표는 왼쪽부터, y좌표

www.acmicpc.net

<풀이>

1. 주어진 입력 값들을 이해하기 쉽게 정리한다.

2. 명령하나씩 실행시키고, 결과를 출력한다.

<해법>

1. 주어진 입력 값들을 이해하기 쉽게 정리하는 방법.

=> 주어진 입력 값이 배열로 되어있지 않습니다. 따라서, 다루기 쉽게 배열의 구조로 바꾸어 주었습니다. 또한, 로봇 위치, 방향 정보를 따로 보관해주고 명령을 실행시킬 때 찾아서 사용하였습니다.

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
#include <iostream>
using namespace std;
 
int di[] = { -1,0,1,0 };
int dj[] = { 0,1,0,-1 };
 
//가로,세로,로봇 개수,명령 개수
int A, B, N, M;
 
//로봇이 움직이는 맵
int map[102][102];
//로봇마다 정보(행,열,방향)
int robotInfo[101][3];
 
bool crash = false;
 
bool simulation(int id, char cmd, int cmdCnt) {
 
    //명령내릴 로봇 위치,방향 저장
    int tmpi = robotInfo[id][0];
    int tmpj = robotInfo[id][1];
    int tmpDir = robotInfo[id][2];
 
    map[tmpi][tmpj] = 0;
 
    //명령 반복 횟수만큼 반복
    while (cmdCnt--) {
        if (cmd == 'L') {
            if (tmpDir == 0) {
                tmpDir = 3;
            }
            else {
                tmpDir--;
            }
        }
        else if (cmd == 'R') {
            if (tmpDir == 3) {
                tmpDir = 0;
            }
            else {
                tmpDir++;
            }
        }
        else {
            tmpi += di[tmpDir];
            tmpj += dj[tmpDir];
 
            //벽에 충돌한 경우
            if (tmpi <= 0 || tmpj <= 0 || tmpi > A || tmpj > B) {
                cout << "Robot " << id << " crashes into the wall" << "\n";
                return true;
            }
 
            //다른 로봇에 충돌한 경우
            if (map[tmpi][tmpj] != 0) {
                cout << "Robot " << id << " crashes into robot " << map[tmpi][tmpj] << "\n";
                return true;
            }
        }
    }
 
    //충돌이 발생하지 않았을 경우, 로봇위치 변경
    map[tmpi][tmpj] = id;
 
    robotInfo[id][0= tmpi;
    robotInfo[id][1= tmpj;
    robotInfo[id][2= tmpDir;
 
    return false;
}
 
int main() {
 
    //1.입력
    cin >> B >> A >> N >> M;
    for (int i = 1; i <= N; i++) {
        int tmpi, tmpj, tmpDir;
        char tmp;
 
        cin >> tmpj >> tmpi >> tmp;
 
        if (tmp == 'N') {
            tmpDir = 0;
        }
        else if (tmp == 'E') {
            tmpDir = 1;
        }
        else if (tmp == 'S') {
            tmpDir = 2;
        }
        else {
            tmpDir = 3;
        }
 
        //배열index 조정
        robotInfo[i][0= (A + 1- tmpi;
        robotInfo[i][1= tmpj;
        robotInfo[i][2= tmpDir;
 
        map[robotInfo[i][0]][robotInfo[i][1]] = i;
    }
    
    //2.명령 수행
    for (int i = 1; i <= M; i++) {
        int id, cmdCnt;
        char cmd;
        cin >> id >> cmd >> cmdCnt;
 
        if (!crash) {
            crash = simulation(id, cmd, cmdCnt);
        }
    }
 
    if (!crash) {
        cout << "OK" << "\n";
    }
 
    return 0;
}

 

시뮬레이션에 대해 알아볼 수 있는 문제였습니다.

+ Recent posts