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 <bits/stdc++.h>
using namespace std;
int n, m;
int xdir[4] = {0, 0, 1, -1};
int ydir[4] = {1, -1, 0, 0};
int ar[1001][1001], visited[1001][1001];
struct point
{
int x, y, d;
};
int bfs(int d)
{
queue<point> q;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m ;j++)
{
if(ar[i][j] == 1)
{
visited[i][j] = 1;
q.push({i, j, d});
}
}
}
int md = 0;
while(!q.empty())
{
md = max(md, q.front().d);
for(int i = 0; i < 4; i++)
{
int nx = q.front().x + xdir[i], ny = q.front().y + ydir[i], nd = q.front().d + 1;
if(0 <= nx && nx < n && 0 <= ny && ny < m)
{
if(!visited[nx][ny] && ar[nx][ny] == 0)
{
visited[nx][ny] = 1;
ar[nx][ny] = 1;
q.push({nx, ny, nd});
}
}
}
q.pop();
}
return md;
}
int main()
{
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> m >> n;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> ar[i][j];
int ans = bfs(0);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(ar[i][j] == 0)
{
cout << "-1";
return 0;
}
}
}
cout << ans;
}
# | 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... |