#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 1010;
int C[N][N];
bool is_up[N][N];
bool is_down[N][N];
int main(){
fastIO;
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= m ; j ++ ){
cin >> C[i][j];
}
}
is_up[1][1]=true;
is_down[n][m]=true;
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= m ; j ++ ){
if(i == 1 && j == 1) continue;
if(C[i][j]==1) continue;
is_up[i][j] = (is_up[i][j-1]|is_up[i-1][j]);
}
}
for(int i = n; i >= 1; i -- ){
for(int j = m ; j >= 1; j -- ){
if(i==n && j == m) continue;
if(C[i][j]==1) continue;
is_down[i][j] = (is_down[i+1][j]|is_down[i][j+1]);
}
}
int q;
cin >> q;
int xi, yi;
bool keep;
int ci, cj;
for(int qr = 1; qr <= q; qr ++ ){
cin >> xi >> yi;
if(!is_up[xi][yi] || !is_down[xi][yi]){
C[xi][yi]=1;
cout << 1 << "\n";
}
else{
keep = false;
for(int i = xi - 1; i >= 1; i -- ){
if(is_up[i][yi] && is_down[i][yi+1]){
keep = true;
}
}
for(int i = yi - 1; i >= 1; i -- ){
if(is_up[xi][i] && is_down[xi+1][i]){
keep = true;
}
}
if(keep == true){
C[xi][yi]=0;
is_up[xi][yi]=false;
is_down[xi][yi]=false;
queue<pii> qq;
qq.push(mp(xi,yi));
while(!qq.empty()){
ci = qq.front().fi;
cj = qq.front().se;
qq.pop();
if(is_up[ci+1][cj] && !is_up[ci+1][cj-1]){
is_up[ci+1][cj]=false;
qq.push(mp(ci+1,cj));
}
if(is_up[ci][cj+1] && !is_up[ci-1][cj+1]){
is_up[ci][cj+1]=false;
qq.push(mp(ci,cj+1));
}
}
qq.push(mp(xi,yi));
while(!qq.empty()){
ci = qq.front().fi;
cj = qq.front().se;
qq.pop();
if(is_down[ci-1][cj] && !is_down[ci-1][cj+1]){
is_down[ci-1][cj]=false;
qq.push(mp(ci-1,cj));
}
if(is_down[ci][cj-1] && !is_down[ci+1][cj-1]){
is_down[ci][cj-1]=false;
qq.push(mp(ci,cj-1));
}
}
cout << "1\n";
}
else{
cout << "0\n";
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
3 ms |
1004 KB |
Output is correct |
4 |
Correct |
4 ms |
1004 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Correct |
5 ms |
1132 KB |
Output is correct |
7 |
Correct |
5 ms |
1132 KB |
Output is correct |
8 |
Correct |
4 ms |
1132 KB |
Output is correct |
9 |
Correct |
4 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
3 ms |
1004 KB |
Output is correct |
4 |
Correct |
4 ms |
1004 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Correct |
5 ms |
1132 KB |
Output is correct |
7 |
Correct |
5 ms |
1132 KB |
Output is correct |
8 |
Correct |
4 ms |
1132 KB |
Output is correct |
9 |
Correct |
4 ms |
1132 KB |
Output is correct |
10 |
Correct |
11 ms |
1004 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
493 ms |
11116 KB |
Output is correct |
13 |
Correct |
90 ms |
7916 KB |
Output is correct |
14 |
Correct |
617 ms |
15468 KB |
Output is correct |
15 |
Correct |
681 ms |
15724 KB |
Output is correct |
16 |
Correct |
376 ms |
16620 KB |
Output is correct |
17 |
Correct |
403 ms |
17516 KB |
Output is correct |
18 |
Correct |
603 ms |
17192 KB |
Output is correct |
19 |
Correct |
423 ms |
18012 KB |
Output is correct |
20 |
Correct |
349 ms |
17900 KB |
Output is correct |
21 |
Correct |
419 ms |
17900 KB |
Output is correct |