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 a[1005][1005], cnt[2005], visited[1005][1005], n, m, o;
queue<pair<int, int> > q;
int f(int x, int y)
{
return (1<=x && x<=n && 1<=y && y<=m);
}
void g()
{
while (!q.empty())
{
int ux=q.front().first, uy=q.front().second;
q.pop();
if (a[ux-1][uy+1])
{
if (f(ux-1, uy) && !a[ux-1][uy])
{
a[ux-1][uy]=1;
cnt[ux+uy-1]--;
q.push({ux-1, uy});
}
if (f(ux, uy+1) && !a[ux][uy+1])
{
a[ux][uy+1]=1;
cnt[ux+uy+1]--;
q.push({ux, uy+1});
}
}
if (a[ux+1][uy-1])
{
if (f(ux+1, uy) && !a[ux+1][uy])
{
a[ux+1][uy]=1;
cnt[ux+uy+1]--;
q.push({ux+1, uy});
}
if (f(ux, uy-1) && !a[ux][uy-1])
{
a[ux][uy-1]=1;
cnt[ux+uy-1]--;
q.push({ux, uy-1});
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=0; i<=m+1; i++)
a[0][i]=a[n+1][i]=1;
for (int i=0; i<=n+1; i++)
a[i][0]=a[i][m+1]=1;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cin >> a[i][j];
if (a[i][j])
q.push({i, j});
else
cnt[i+j]++;
}
}
g();
cin >> o;
for (int i=1; i<=o; i++)
{
int x, y;
cin >> x >> y;
if (a[x][y])
cout << 1 << '\n';
else if (cnt[x+y]==1)
cout << 0 << '\n';
else
{
cout << 1 << '\n';
a[x][y]=1;
cnt[x+y]--;
q.push({x, y});
g();
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |