[BOJ] 16954 움직이는 미로 탈출

Time Lapse :NONE

16954.cpp

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
#include <stdio.h>
#include <queue>
using namespace std;
char buf[8][9];
int dx[9] = { -1,0,1,-1,0,1,-1,0,1 };
int dy[9] = { -1,-1,-1,0,0,0,1,1,1 };
int t = 0;
int main() {
for (int i = 0; i < 8; ++i)
scanf("%s", buf[i]);
queue<pair<int, int>> q;
q.emplace(7, 0);
while (!q.empty()) {
int size = q.size();
int visit[8][8] = { 0, };
for (int s = 0; s < size; ++s) {
int x = q.front().second;
int y = q.front().first;
q.pop();
if (buf[y][x] == '#') continue;
for (int i = 0; i < 9; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < 8 && 0 <= ny && ny < 8 && buf[ny][nx] == '.' && !visit[ny][nx]) {
visit[ny][nx] = 1;
if (ny<t) {
printf("1"); return 0;
}
q.emplace(ny, nx);
}
}
}
for (int i = 0; i < 8; ++i)
if (buf[7][i] == '#') buf[7][i] = '.';
for (int i = 6; i >= 0; --i) {
for (int j = 0; j < 8; ++j) {
if (buf[i][j] == '#') {
buf[i + 1][j] = '#';
buf[i][j] = '.';
}
}
}
++t;
}
printf("0");
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/16954/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.