-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon_16236.cpp
68 lines (51 loc) · 1.63 KB
/
baekjoon_16236.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <queue>
using namespace std;
struct node {
int c, r, d;
node(int col, int row, int dist) : c(col), r(row), d(dist) {}
bool operator < (const node& p)const {
if(d == p.d) {
if(c == p.c) return r > p.r;
return c > p.c;
}
return d > p.d;
}
};
int _size, dc[] = {-1, 0, 0, 1}, dr[] = {0, -1, 1, 0}, map[20][20], col, row, N, res, ans, need;
bool isPossible(int c, int r) { return ( c >= 0 && r >= 0 && c < N && r < N && _size >= map[c][r] ); }
int bfs() {
priority_queue<node> pos;
bool visited[20][20] = {false,};
visited[col][row] = true;
pos.push(node(col, row, 0));
while(!pos.empty()) {
auto now = pos.top(); pos.pop();
if(map[now.c][now.r] && map[now.c][now.r] < _size) {
map[now.c][now.r] = 0;
col = now.c; row = now.r;
if(--need == 0) need = ++_size;
return now.d;
}
for(int w = 0; w < 4; w++) {
int nxtC = now.c + dc[w], nxtR = now.r + dr[w];
if(!isPossible(nxtC, nxtR) || visited[nxtC][nxtR]) continue;
visited[nxtC][nxtR] = true;
pos.push(node(nxtC, nxtR, now.d + 1));
}
}
return 0;
}
int main() { cin.tie(0); cout.tie(0)->sync_with_stdio(0);
cin >> N;
for(int c = 0; c < N; c++) {
for(int r = 0; r < N; r++) {
cin >> map[c][r];
if(map[c][r] == 9) { col = c; row = r; map[c][r] = 0; }
}
}
_size = need = 2;
while(res = bfs()) ans += res;
cout << ans;
return 0;
}