본문 바로가기
Programming/Algorithm

swe 1953 탈주범 검거

by OKOK 2018. 4. 11.

1. bfs + 시뮬입니다.

2. 어떻게 할 것인지, 그리고 처음에 설계를 하고 예외가 없는지 체크를 해보았습니다. 괜찮다고 판단이 되어 설계한 대로 구현했습니다.

3. 초가 지나가는 것에 대해서 와일문을 쓰고, 큐 엠티를 쓰고, 이것을 적용해서 가산점 고고


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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
1030 시작
1시간 컷
1. 비지트로 하고, 할 때마다 답을 +1 을 합니다.
2. 갈 수 있는 길을 알아보는 것,
지금 시작 위치가 3이기때문에 왼쪽, 오른쪽을 검사하고, 갈 수 있으면 비지트를 합니다.
왼쪽이 무엇이면ㅇ
*/
 
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define SIZE 55 // 55
int N, M, L, R, C;
int map[SIZE][SIZE];
int visit[SIZE][SIZE];
int ans;
int num;
int sec = 1;
struct point {
    int x, y;
};
queue<point> que;
int x, y, nx, ny;
 
void problemIn() {
    cin >> N >> M >> R >> C >> L;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
        }
    }
}
 
void init() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            map[i][j] = 0;
            visit[i][j] = 0;
        }
    }
    while (!que.empty()) que.pop();
    sec = 1;
    ans = 0;
}
 
void bfs() {
 
    while (!que.empty()) {
        int qlen = que.size();
 
 
        if (sec == L) {
            return;
        }
 
        while (qlen--) {
            x = que.front().x;
            y = que.front().y;
            num = map[x][y];
            que.pop();
 
            if (num == 1) { // 위, 아래, 오른쪽, 왼쪽
                if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
                    if (visit[x - 1][y] == 0) {
                        visit[x - 1][y] = 1;
                        ans++;
                        que.push({ x - 1, y });
                    }
                }
                if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
                    if (visit[x + 1][y] == 0) {
                        visit[x + 1][y] = 1;
                        ans++;
                        que.push({ x + 1, y });
                    }
                }
                if (map[x][y + 1== 1 || map[x][y + 1== 6 || map[x][y + 1== 7 || map[x][y + 1== 3) {
                    if (visit[x][y + 1== 0) {
                        visit[x][y + 1= 1;
                        ans++;
                        que.push({ x, y + 1 });
                    }
                }
                if (map[x][y - 1== 1 || map[x][y - 1== 3 || map[x][y - 1== 4 || map[x][y - 1== 5) {
                    if (visit[x][y - 1== 0) {
                        visit[x][y - 1= 1;
                        ans++;
                        que.push({ x, y - 1 });
                    }
                }
            }
            else if (num == 2) { // 위, 아래
                if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
                    if (visit[x - 1][y] == 0) {
                        visit[x - 1][y] = 1;
                        ans++;
                        que.push({ x - 1, y });
                    }
                }
                if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
                    if (visit[x + 1][y] == 0) {
                        visit[x + 1][y] = 1;
                        ans++;
                        que.push({ x + 1, y });
                    }
                }
            }
            else if (num == 3) { // 오른쪽, 왼쪽
                if (map[x][y + 1== 1 || map[x][y + 1== 6 || map[x][y + 1== 7 || map[x][y + 1== 3) {
                    if (visit[x][y + 1== 0) {
                        visit[x][y + 1= 1;
                        ans++;
                        que.push({ x, y + 1 });
                    }
                }
                if (map[x][y - 1== 1 || map[x][y - 1== 3 || map[x][y - 1== 4 || map[x][y - 1== 5) {
                    if (visit[x][y - 1== 0) {
                        visit[x][y - 1= 1;
                        ans++;
                        que.push({ x, y - 1 });
                    }
                }
            }
            else if (num == 4) { // 위, 오른쪽
                if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
                    if (visit[x - 1][y] == 0) {
                        visit[x - 1][y] = 1;
                        ans++;
                        que.push({ x - 1, y });
                    }
                }
                if (map[x][y + 1== 1 || map[x][y + 1== 6 || map[x][y + 1== 7 || map[x][y + 1== 3) {
                    if (visit[x][y + 1== 0) {
                        visit[x][y + 1= 1;
                        ans++;
                        que.push({ x, y + 1 });
                    }
                }
            }
            else if (num == 5) { // 오른쪽, 아래
                if (map[x][y + 1== 1 || map[x][y + 1== 6 || map[x][y + 1== 7 || map[x][y + 1== 3) {
                    if (visit[x][y + 1== 0) {
                        visit[x][y + 1= 1;
                        ans++;
                        que.push({ x, y + 1 });
                    }
                }
                if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
                    if (visit[x + 1][y] == 0) {
                        visit[x + 1][y] = 1;
                        ans++;
                        que.push({ x + 1, y });
                    }
                }
            }
            else if (num == 6) { // 왼쪽, 아래
                if (map[x][y - 1== 1 || map[x][y - 1== 3 || map[x][y - 1== 4 || map[x][y - 1== 5) {
                    if (visit[x][y - 1== 0) {
                        visit[x][y - 1= 1;
                        ans++;
                        que.push({ x, y - 1 });
                    }
                }
                if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
                    if (visit[x + 1][y] == 0) {
                        visit[x + 1][y] = 1;
                        ans++;
                        que.push({ x + 1, y });
                    }
                }
            }
            else if (num == 7) { // 왼쪽, 위에
                if (map[x][y - 1== 1 || map[x][y - 1== 3 || map[x][y - 1== 4 || map[x][y - 1== 5) {
                    if (visit[x][y - 1== 0) {
                        visit[x][y - 1= 1;
                        ans++;
                        que.push({ x, y - 1 });
                    }
                }
                if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
                    if (visit[x - 1][y] == 0) {
                        visit[x - 1][y] = 1;
                        ans++;
                        que.push({ x - 1, y });
                    }
                }
            } // end else if
        } // end qlen
        
//        if (sec == (L-1)) {
//            return;
//        }
        sec++;
    } // end que.empty
}
 
void solve() {
 
    visit[R][C] = 1;
    ans++;
    que.push({ R,C });
    bfs();
}
 
int main() {
    int T;
    cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        problemIn();
        solve();
        cout << "#" << tc << " " << ans << endl;
        init();
    }
    return 0;
}
cs