This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |