#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
vector<vector<ll>> cad;
vector<set<pair<ll,ll>>> s;
ll dx[]={1,0,-1,0};
ll dy[]={0,1,0,-1};
void dfs(ll a, ll b)
{
if((a==1&&b==1)||(a==n&&b==m)||cad[a][b]==1) return;
cad[a][b]=(cad[a-1][b]&cad[a][b-1])|(cad[a+1][b]&cad[a][b+1]);
if(cad[a][b]==0) return;
s[a+b].erase({a,b});
for(int i=0; i<4; i++)
dfs(a+dx[i],b+dy[i]);
}
ll f(ll a, ll b)
{
if(s[a+b].size()-(s[a+b].find({a,b})!=s[a+b].end())<1) return 0;
cad[a][b]=1;
s[a+b].erase({a,b});
for(int i=0; i<4; i++)
dfs(a+dx[i],b+dy[i]);
return 1;
}
int main()
{
cin>>n>>m;
cad.assign(n+2,vector<ll>(m+2,1));
s.resize(n+m+5);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
s[i+j].insert({i,j});
cad[i][j]=0;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
ll temp;
cin>>temp;
if(temp) f(i,j);
}
}
ll q;
cin>>q;
while(q--)
{
ll a,b;
cin>>a>>b;
cout<<f(a,b)<<"\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |