반응형
https://www.acmicpc.net/problem/7576
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
|
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int M, N;
int arr[1002][1002];
int MAX_value=0;
int current_value=0;
void BFS(int y,int x,queue <pair<int,int>> &q_locate,queue<int> &q_current) {
if (y >= N || x >= M || x < 0 || y < 0) return;
if (arr[y][x] == 0) {
current_value++;
arr[y][x] = q_current.front() + 1;
}
}
int main(void) {
cin >> M >> N;
queue <pair<int, int>> q_locate;
queue <int> q_current;
MAX_value = M * N;
for (int i = 0;i < N;i++) {
for (int j = 0;j < M;j++) {
cin >> arr[i][j];
if (arr[i][j] == 1) {
current_value++;
}
else if (arr[i][j] == -1) {
MAX_value--;
}
}
}
int current=1;
BFS(q_locate.front().first-1, q_locate.front().second,q_locate,q_current);
BFS(q_locate.front().first+1, q_locate.front().second, q_locate, q_current);
BFS(q_locate.front().first, q_locate.front().second-1, q_locate, q_current);
BFS(q_locate.front().first, q_locate.front().second+1, q_locate, q_current);
current = q_current.front();
q_current.pop();
q_locate.pop();
}
if (current_value == MAX_value)
cout << current - 1 << endl;
else
cout << -1 << endl;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형