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;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int n,m;
int it[1001][1001];
int vis[1001][1001];
int vis2[1001][1001];
int val[1001][1001];
int co[3001];
int cal(int i,int j){
int co=0;
if(i+1<n){
co+=val[i+1][j];
}
if(j+1<m){
co+=val[i][j+1];
}
return co;
}
int cal2(int i,int j){
int co=0;
if(i>0){
co+=val[i-1][j];
}
if(j>0){
co+=val[i][j-1];
}
return co;
}
void dfs(int aa,int bb){
val[aa][bb]=0;
co[aa+bb]-=1;
if(aa>0){
if(val[aa-1][bb]){
if(cal(aa-1,bb)==0){
dfs(aa-1,bb);
}
}
}
if(bb>0){
if(val[aa][bb-1]){
if(cal(aa,bb-1)==0){
dfs(aa,bb-1);
}
}
}
}
void dfs2(int aa,int bb){
val[aa][bb]=0;
co[aa+bb]-=1;
if(aa<n-1){
if(val[aa+1][bb]){
if(cal2(aa+1,bb)==0){
dfs2(aa+1,bb);
}
}
}
if(bb<m-1){
if(val[aa][bb+1]){
if(cal2(aa,bb+1)==0){
dfs2(aa,bb+1);
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>it[i][j];
}
}
vis[n-1][m-1]=1;
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
if(i==n-1 and j==m-1){
continue;
}
if(it[i][j]==0){
if(i<n-1){
vis[i][j]|=vis[i+1][j];
}
if(j<m-1){
vis[i][j]|=vis[i][j+1];
}
}
}
}
vis2[0][0]=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(it[i][j]==1){
continue;
}
if(i==0 and j==0){
continue;
}
if(i>0){
vis2[i][j]|=vis2[i-1][j];
}
if(j>0){
vis2[i][j]|=vis2[i][j-1];
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis2[i][j] and vis[i][j]){
val[i][j]=1;
co[i+j]+=1;
}
}
}
int q;
cin>>q;
while(q--){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
if(val[aa][bb]==0){
cout<<1<<endl;
continue;
}
if(co[aa+bb]==1){
cout<<0<<endl;
continue;
}
else{
dfs(aa,bb);
dfs2(aa,bb);
co[aa+bb]+=1;
cout<<1<<endl;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |