#include <bits/stdc++.h>
#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
#ifdef D
#define debug(x) cout << x
#else
#define debug(x) //nothing
#endif
const int MAXN = 100+5;
vector<pair<ll,ll>> oper = {{0,1},{1,0}};
vector<pair<ll,ll>> roper = {{0,-1},{-1,0}};
ll n,m;
vector<vector<ll>> mapa;
bool vis[MAXN][MAXN];
vector<pair<ll,ll>> llega[MAXN][MAXN];
bool rright[MAXN][MAXN];
bool down[MAXN][MAXN];
bool dfs(ll i, ll j){
if(vis[i][j]) return (rright[i][j]||down[i][j]);
vis[i][j]=true;
if(i==n-1 && j==m-1) rright[i][j]=down[i][j]=true;
for(auto o:oper) if(i+o.fst<n && j+o.snd<m){
if(mapa[i+o.fst][j+o.snd]==0 && dfs(i+o.fst,j+o.snd)){
if(o.snd==1) rright[i][j]=true;
else down[i][j]=true;
}
}
return (rright[i][j]||down[i][j]);
}
bool yes=false;
vector<pair<pair<ll,ll>,ll>> era;
void rdfs(ll i, ll j){
if(i==0 && j==0){ yes=true; return; }
debug("rdfs "<<i<<" "<<j<<'\n');
bool parc = true;
for(auto o:roper) if(i+o.fst>=0 && j+o.snd>=0){
if(o.snd==-1 && rright[i+o.fst][j+o.snd]){
rright[i+o.fst][j+o.snd]=false;
parc=false;
era.pb({{i+o.fst,j+o.snd},0});
}
if(o.fst==-1 && down[i+o.fst][j+o.snd]){
down[i+o.fst][j+o.snd]=false;
parc=false;
era.pb({{i+o.fst,j+o.snd},1});
}
if(!rright[i+o.fst][j+o.snd] && !down[i+o.fst][j+o.snd] && !parc){
rdfs(i+o.fst,j+o.snd);
}
}
}
int main(){ FIN;
cin>>n>>m;
mapa.clear();
mapa.resize(n,vector<ll>(m,0));
forn(i,0,n){
forn(j,0,m){
cin>>mapa[i][j];
}
}
dfs(0,0);
forn(I,0,n){
forn(J,0,m){
debug(mapa[I][J]<<" ("); debug(rright[I][J]<<","<<down[I][J]); debug(") ");
} debug('\n');
}
ll q; cin>>q;
forn(i,0,q){
ll x,y; cin>>x>>y; x--; y--;
if(mapa[x][y]!=1){
mapa[x][y]=1;
/*mset(vis,false);
forn(I,0,n) forn(J,0,n) llega[I][J].clear();
if(!dfs(0,0)) mapa[x][y]=0, cout<<0<<'\n';
else cout<<1<<'\n';*/
debug('\n');
debug('\n');
debug(x<<" "<<y<<'\n');
yes=false;
rdfs(x,y);
debug(yes<<'\n');
if(yes){
mapa[x][y]=0;
forn(j,0,SZ(era)){
if(era[j].snd) down[era[j].fst.fst][era[j].fst.snd]=true;
else rright[era[j].fst.fst][era[j].fst.snd]=true;
}
era.clear();
cout<<0<<'\n';
}else{
era.clear();
rright[x][y]=false;
down[x][y]=false;
cout<<1<<'\n';
}
forn(I,0,n){
forn(J,0,m){
debug(mapa[I][J]<<" ("); debug(rright[I][J]<<","<<down[I][J]); debug(") ");
} debug('\n');
}
}else{
cout<<0<<'\n';
}
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |