-
Notifications
You must be signed in to change notification settings - Fork 0
/
16236.cpp
74 lines (69 loc) · 1.46 KB
/
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
69
70
71
72
73
74
#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
using namespace std;
int n;
int map[21][21] = {0, };
bool visited[21][21] = {0, };
int sharkSize = 2;
int xp = 0;
int answer = 0;
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>>q;
void bfs();
int dy[4] = {0, 0, -1, 1};
int dx[4] = {-1, 1, 0, 0};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> map[i][j];
if(map[i][j] == 9){
visited[i][j] = 1;
map[i][j] = 0;
q.push({0, i, j});
}
}
}
bfs();
cout << answer;
}
void bfs(){
while (!q.empty()){
int nowDist = get<0>(q.top());
int nowY = get<1>(q.top());
int nowX = get<2>(q.top());
q.pop();
if(map[nowY][nowX] < sharkSize && map[nowY][nowX] >= 1){
xp++;
if(xp == sharkSize){
sharkSize++;
xp = 0;
}
answer += nowDist;
map[nowY][nowX] = 0;
while(!q.empty())q.pop();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
visited[i][j] = 0;
}
}
visited[nowY][nowX] = 1;
q.push({0, nowY, nowX});
continue;
}
for(int i = 0; i < 4; i++){
int nextY = nowY + dy[i];
int nextX = nowX + dx[i];
if(nextY < 0 || nextX < 0 || nextY >= n || nextX >= n)continue;
if(map[nextY][nextX] > sharkSize)continue;
if(visited[nextY][nextX] == 0){
visited[nextY][nextX] = 1;
q.push({nowDist+1, nextY, nextX});
}
}
}
}