#include <bits/stdc++.h>
#define MAXN 1007
using namespace std;
bool dpgl[MAXN][MAXN],dpdd[MAXN][MAXN],b[MAXN][MAXN];
int d[2*MAXN];
int n,m;
void theboysgl(int x,int y)
{
if(x==n+1 || y==m+1 || !dpgl[x][y] || !((x==1 || !dpgl[x-1][y]) && (y==1 || !dpgl[x][y-1]))) return;
if(dpdd[x][y]) d[x+y]--;
dpgl[x][y]=false;
theboysgl(x+1,y); theboysgl(x,y+1);
}
void theboysdd(int x,int y)
{
if(x==0 || y==0 || !dpdd[x][y] || !((x==n || !dpdd[x+1][y]) && (y==m || !dpdd[x][y+1]))) return;
if(dpgl[x][y]) d[x+y]--;
dpdd[x][y]=false;
theboysdd(x-1,y); theboysdd(x,y-1);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>b[i][j];
dpgl[1][1]=true; dpdd[n][m]=1;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if((i!=1 || j!=1) && !b[i][j]) dpgl[i][j]=dpgl[i-1][j] || dpgl[i][j-1];
for(int i=n;i>=1;i--) for(int j=m;j>=1;j--) if((i!=n || j!=m) && !b[i][j]) dpdd[i][j]=dpdd[i+1][j] || dpdd[i][j+1];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(dpgl[i][j] && dpdd[i][j]) d[i+j]++;
int q; cin>>q;
while(q--)
{
int x,y; cin>>x>>y;
if(d[x+y]==1 && dpdd[x][y] && dpgl[x][y]) {cout<<"0\n"; continue;}
cout<<"1\n";
if(dpdd[x][y] && dpgl[x][y]) d[x+y]--;
dpdd[x][y]=dpgl[x][y]=false;
theboysgl(x+1,y); theboysgl(x,y+1);
theboysdd(x-1,y); theboysdd(x,y-1);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
11 ms |
640 KB |
Output is correct |
3 |
Correct |
14 ms |
640 KB |
Output is correct |
4 |
Correct |
25 ms |
640 KB |
Output is correct |
5 |
Correct |
31 ms |
640 KB |
Output is correct |
6 |
Correct |
33 ms |
640 KB |
Output is correct |
7 |
Correct |
33 ms |
640 KB |
Output is correct |
8 |
Correct |
32 ms |
640 KB |
Output is correct |
9 |
Correct |
32 ms |
684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
11 ms |
640 KB |
Output is correct |
3 |
Correct |
14 ms |
640 KB |
Output is correct |
4 |
Correct |
25 ms |
640 KB |
Output is correct |
5 |
Correct |
31 ms |
640 KB |
Output is correct |
6 |
Correct |
33 ms |
640 KB |
Output is correct |
7 |
Correct |
33 ms |
640 KB |
Output is correct |
8 |
Correct |
32 ms |
640 KB |
Output is correct |
9 |
Correct |
32 ms |
684 KB |
Output is correct |
10 |
Correct |
86 ms |
692 KB |
Output is correct |
11 |
Correct |
20 ms |
512 KB |
Output is correct |
12 |
Correct |
1184 ms |
4088 KB |
Output is correct |
13 |
Correct |
287 ms |
3324 KB |
Output is correct |
14 |
Correct |
2792 ms |
5240 KB |
Output is correct |
15 |
Correct |
2925 ms |
5124 KB |
Output is correct |
16 |
Correct |
3189 ms |
5096 KB |
Output is correct |
17 |
Correct |
3381 ms |
5440 KB |
Output is correct |
18 |
Correct |
3316 ms |
5312 KB |
Output is correct |
19 |
Correct |
3383 ms |
5316 KB |
Output is correct |
20 |
Correct |
3417 ms |
5496 KB |
Output is correct |
21 |
Correct |
3428 ms |
5460 KB |
Output is correct |