#include <cstdio>
#include <queue>
int m,n,h;
int a[1001][1001];
int depth[1001][1001];
int v[1001][1001];
std::queue<int> x,y,d;
void f(int i, int j, int dt){
v[i][j]=1;
depth[i][j]=dt;
x.push(i);
y.push(j);
d.push(dt);
}
int main(){
scanf("%d %d",&n,&m);
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
scanf("%d",&a[i][j]);
if(a[i][j]==1){
f(i,j,0);
}
if(a[i][j]==-1){
v[i][j]=1;
}
}
}
int cx,cy,cd;
while (!x.empty()) {
cx = x.front();
cy = y.front();
cd = d.front();
if(cx && !v[cx-1][cy]){
f(cx-1,cy,cd+1);
}
if(cy && !v[cx][cy-1]){
f(cx,cy-1,cd+1);
}
if(cx+1<m && !v[cx+1][cy]){
f(cx+1,cy,cd+1);
}
if(cy+1<n && !v[cx][cy+1]){
f(cx,cy+1,cd+1);
}
x.pop();
y.pop();
d.pop();
}
int dtmax = 0;
int unvisited = 0;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if(dtmax<depth[i][j]) dtmax = depth[i][j];
if(v[i][j]==0) unvisited = 1;
}
}
if(unvisited) printf("-1");
else printf("%d",dtmax);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
12980 KB |
Output is correct |
2 |
Correct |
0 ms |
12980 KB |
Output is correct |
3 |
Correct |
0 ms |
12980 KB |
Output is correct |
4 |
Correct |
0 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
12980 KB |
Output is correct |
2 |
Correct |
0 ms |
12980 KB |
Output is correct |
3 |
Correct |
0 ms |
12980 KB |
Output is correct |
4 |
Correct |
0 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
12980 KB |
Output is correct |
2 |
Correct |
0 ms |
12980 KB |
Output is correct |
3 |
Correct |
0 ms |
12980 KB |
Output is correct |
4 |
Correct |
0 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12980 KB |
Output is correct |
2 |
Correct |
0 ms |
12980 KB |
Output is correct |
3 |
Correct |
0 ms |
12980 KB |
Output is correct |
4 |
Correct |
0 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12980 KB |
Output is correct |
2 |
Correct |
0 ms |
12980 KB |
Output is correct |
3 |
Correct |
4 ms |
12980 KB |
Output is correct |
4 |
Correct |
0 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
12980 KB |
Output is correct |
2 |
Correct |
4 ms |
12980 KB |
Output is correct |
3 |
Correct |
0 ms |
12980 KB |
Output is correct |
4 |
Correct |
4 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
12980 KB |
Output is correct |
2 |
Correct |
16 ms |
12980 KB |
Output is correct |
3 |
Correct |
8 ms |
12980 KB |
Output is correct |
4 |
Correct |
16 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
12980 KB |
Output is correct |
2 |
Correct |
48 ms |
13112 KB |
Output is correct |
3 |
Correct |
8 ms |
12980 KB |
Output is correct |
4 |
Correct |
8 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
12980 KB |
Output is correct |
2 |
Correct |
84 ms |
12980 KB |
Output is correct |
3 |
Correct |
12 ms |
12980 KB |
Output is correct |
4 |
Correct |
64 ms |
12980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
13112 KB |
Output is correct |
2 |
Correct |
128 ms |
13244 KB |
Output is correct |
3 |
Correct |
120 ms |
12980 KB |
Output is correct |
4 |
Correct |
92 ms |
12980 KB |
Output is correct |