#include <bits/stdc++.h>
using namespace std;
struct pos{
int x,y;
};
int N,M,Q;
bool A[1001][1001];
bool DP1[1001][1001],DP2[1002][1002];
int Cnt[2001];
int dx[2]={0,1},dy[2]={1,0};
void solve(int qx,int qy){
if (DP1[qx][qy]&&DP2[qx][qy]&&Cnt[qx+qy]==1){
cout<<"0\n";
return;
}
A[qx][qy]=true;
if (DP1[qx][qy]&&DP2[qx][qy])
Cnt[qx+qy]--;
DP1[qx][qy]=false;
DP2[qx][qy]=false;
queue<pos> q;
q.push({qx,qy});
while (!q.empty()){
pos t=q.front();
q.pop();
for (int i=0; i<2; i++){
int x=t.x+dx[i],y=t.y+dy[i];
if (x>N||y>M||A[x][y]||!DP1[x][y]||DP1[x-1][y]||DP1[x][y-1])
continue;
if (DP1[x][y]&&DP2[x][y])
Cnt[x+y]--;
DP1[x][y]=false;
q.push({x,y});
}
}
q.push({qx,qy});
while (!q.empty()){
pos t=q.front();
q.pop();
for (int i=0; i<2; i++){
int x=t.x-dx[i],y=t.y-dy[i];
if (!x||!y||A[x][y]||!DP2[x][y]||DP2[x+1][y]||DP2[x][y+1])
continue;
if (DP1[x][y]&&DP2[x][y])
Cnt[x+y]--;
DP2[x][y]=false;
q.push({x,y});
}
}
cout<<"1\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>N>>M;
for (int i=1; i<=N; i++){
for (int j=1; j<=M; j++)
cin>>A[i][j];
}
DP1[1][1]=true;
for (int i=1; i<=N; i++){
for (int j=1; j<=M; j++){
if (!A[i][j]&&(DP1[i-1][j]||DP1[i][j-1]))
DP1[i][j]=true;
}
}
DP2[N][M]=true;
for (int i=N; i; i--){
for (int j=M; j; j--){
if (!A[i][j]&&(DP2[i+1][j]||DP2[i][j+1]))
DP2[i][j]=true;
}
}
for (int i=1; i<=N; i++){
for (int j=1; j<=M; j++){
if (DP1[i][j]&&DP2[i][j])
Cnt[i+j]++;
}
}
cin>>Q;
while (Q--){
int x,y;
cin>>x>>y;
solve(x,y);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
3 ms |
696 KB |
Output is correct |
5 |
Correct |
4 ms |
692 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
6 ms |
728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
3 ms |
696 KB |
Output is correct |
5 |
Correct |
4 ms |
692 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
6 ms |
728 KB |
Output is correct |
10 |
Correct |
15 ms |
808 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
164 ms |
7788 KB |
Output is correct |
13 |
Correct |
62 ms |
4932 KB |
Output is correct |
14 |
Correct |
322 ms |
12528 KB |
Output is correct |
15 |
Correct |
324 ms |
12764 KB |
Output is correct |
16 |
Correct |
382 ms |
13624 KB |
Output is correct |
17 |
Correct |
343 ms |
14476 KB |
Output is correct |
18 |
Correct |
352 ms |
14040 KB |
Output is correct |
19 |
Correct |
325 ms |
14804 KB |
Output is correct |
20 |
Correct |
308 ms |
14888 KB |
Output is correct |
21 |
Correct |
335 ms |
14772 KB |
Output is correct |