#include <bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define all(x) x.begin(), x.end()
#define sz(a) ((int) a.size())
#define pb push_back
#define fst first
#define snd second
using namespace std;
typedef long long ll;
const int N=1005;
int C[N][N],n,m;
bool vis[N][N];
bool safe(int i,int j){
return i>=0&&i<n&&j>=0&&j<m;
}
void dfs(int x,int y){
vis[x][y]=true;
int i=x+1,j=y;
if(safe(i,j)&&!vis[i][j]&&C[i][j]!=1){
dfs(i,j);
}
i=x,j=y+1;
if(safe(i,j)&&!vis[i][j]&&C[i][j]!=1){
dfs(i,j);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
L(i,0,n-1)L(j,0,m-1){
cin>>C[i][j];
}
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;
x--;y--;
memset(vis,0,sizeof(vis));
C[x][y]=1;
dfs(0,0);
if(vis[n-1][m-1])cout<<1<<endl;
else{
C[x][y]=0;
cout<<0<<endl;
}
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |