#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=1005,mod=1e9+7;
int t,d2[N][N],d1[N][N],p[N][N],n,m,cnt[2*N];
queue<pair<int,int> > q;
char c[N][N];
bool ok(int x,int y){
if(min(x,y)<1 || x>n || y>m) return 0;
return 1;
}
void dfs1(int x,int y){
if(d1[x][y] || c[x][y]=='1') return;
d1[x][y]=1;
if(ok(x+1,y)) dfs1(x+1,y);
if(ok(x,y+1)) dfs1(x,y+1);
}
void dfs2(int x,int y){
if(d2[x][y] || c[x][y]=='1') return;
d2[x][y]=1;
if(ok(x-1,y)) dfs2(x-1,y);
if(ok(x,y-1)) dfs2(x,y-1);
}
void update1(int x,int y){
if(ok(x+1,y) && p[x+1][y] && !p[x+1][y-1]) q.push({x+1,y});
if(ok(x,y+1) && p[x][y+1] && !p[x-1][y+1]) q.push({x,y+1});
while(q.size()){
x=q.front().f;
y=q.front().s;
q.pop();
if(p[x][y-1] || p[x-1][y]) continue;
p[x][y]=0; cnt[x+y]--;
if(ok(x+1,y) && p[x+1][y] && !p[x+1][y-1]) q.push({x+1,y});
if(ok(x,y+1) && p[x][y+1] && !p[x-1][y+1]) q.push({x,y+1});
}
}
void update2(int x,int y){
if(ok(x-1,y) && p[x-1][y] && !p[x-1][y+1]) q.push({x-1,y});
if(ok(x,y-1) && p[x][y-1] && !p[x+1][y-1]) q.push({x,y-1});
while(q.size()){
x=q.front().f;
y=q.front().s;
q.pop();
if(p[x][y+1] || p[x+1][y]) continue;
p[x][y]=0; cnt[x+y]--;
if(ok(x-1,y) && p[x-1][y] && !p[x-1][y+1]) q.push({x-1,y});
if(ok(x,y-1) && p[x][y-1] && !p[x+1][y-1]) q.push({x,y-1});
}
}
main(){
// t=1;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c[i][j];
}
}
dfs1(1,1);
dfs2(n,m); d1[1][1]=d2[n][m]=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(d1[i][j] && d2[i][j]) p[i][j]=1,cnt[i+j]++;
}
}
int q;
cin>>q;
while(q--){
int a,b;
cin>>a>>b;
if(!p[a][b] ) {
cout<<1<<endl;
}
else {
if(cnt[a+b]-1){
p[a][b]=0;
cnt[a+b]--;
update1(a,b);
update2(a,b);
cout<<1<<endl;
}
else cout<<0<<endl;
}
}
}
Compilation message
furniture.cpp:52:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1356 KB |
Output is correct |
2 |
Correct |
9 ms |
1612 KB |
Output is correct |
3 |
Correct |
10 ms |
1644 KB |
Output is correct |
4 |
Correct |
18 ms |
1616 KB |
Output is correct |
5 |
Correct |
24 ms |
1792 KB |
Output is correct |
6 |
Correct |
22 ms |
1864 KB |
Output is correct |
7 |
Correct |
23 ms |
1860 KB |
Output is correct |
8 |
Correct |
22 ms |
1864 KB |
Output is correct |
9 |
Correct |
26 ms |
1864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1356 KB |
Output is correct |
2 |
Correct |
9 ms |
1612 KB |
Output is correct |
3 |
Correct |
10 ms |
1644 KB |
Output is correct |
4 |
Correct |
18 ms |
1616 KB |
Output is correct |
5 |
Correct |
24 ms |
1792 KB |
Output is correct |
6 |
Correct |
22 ms |
1864 KB |
Output is correct |
7 |
Correct |
23 ms |
1860 KB |
Output is correct |
8 |
Correct |
22 ms |
1864 KB |
Output is correct |
9 |
Correct |
26 ms |
1864 KB |
Output is correct |
10 |
Correct |
53 ms |
1544 KB |
Output is correct |
11 |
Correct |
14 ms |
1100 KB |
Output is correct |
12 |
Correct |
743 ms |
25000 KB |
Output is correct |
13 |
Correct |
123 ms |
23236 KB |
Output is correct |
14 |
Correct |
1849 ms |
24732 KB |
Output is correct |
15 |
Correct |
1982 ms |
33020 KB |
Output is correct |
16 |
Correct |
2117 ms |
34772 KB |
Output is correct |
17 |
Correct |
2201 ms |
36156 KB |
Output is correct |
18 |
Correct |
2120 ms |
35012 KB |
Output is correct |
19 |
Correct |
2301 ms |
36732 KB |
Output is correct |
20 |
Correct |
2228 ms |
36436 KB |
Output is correct |
21 |
Correct |
2293 ms |
36636 KB |
Output is correct |