#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
typedef long long ll;
vector<vector<int>>aux;
int dX[]={0,1};
int dY[]={1,0};
int n,m;
bool f=false;
bool dfs(int x,int y,vector<vector<int>>grid){
grid[x][y]=1;
if(x==n-1 && y==m-1){
f=true;
}
for(int i=0;i<2;i++){
int nX=x+dX[i],nY=y+dY[i];
if(nX>=0 && nY>=0 && nX<n && nY<m){
if(grid[nX][nY]!=1){
//cout<<nX<<" "<<nY<<"\n";
dfs(nX,nY,grid);
}
}
}
return f;
}
void solve(){
cin>>n>>m;
//cout<<"HOLA\n";
aux.assign(n,vector<int>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>aux[i][j];
}
}
//cout<<"HOLA\n";
int q; cin>>q;
if(aux[0][0]==1 || aux[n-1][m-1]==1 || dfs(0,0,aux)==0){
//cout<<"UYY";
while(q--){
int x,y; cin>>x>>y;
cout<<"0\n";
}
return;
}
while(q--){
//cout<<"HOLA\n";
int x,y; cin>>x>>y;
x--;
y--;
aux[x][y]=1;
f=false;
if(dfs(0,0,aux)==true){
cout<<"1\n";
}
else{
cout<<"0\n";
aux[x][y]=0;
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |