#include<bits/stdc++.h>
// #include<atcoder/modint>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rs resize
#define ft front()
#define bk back()
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define r0f(i,n) for(int i = n-1; i >= 0; i--)
#define R0F(i,k,n) for(int i = n-1; i >= k; i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define pout(a) cout<<a.F<<' '<<a.S<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<endl
#define dout(a) cout<<a<<' '<<#a<<endl
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<endl
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
#define p2(x) (1LL << (x))
#define pct(x) (__builtin_popcountll(x))
#define ist(x,y) ((x >> y) & 1)
template<class T, class U> bool ckmin(T& a, const U& b) { return (T)b < a ? a = (T)b, 1 : 0; }
template<class T, class U> bool ckmax(T& a, const U& b) { return a < (T)b ? a = (T)b, 1 : 0; }
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
// using namespace atcoder;
// using mint = modint998244353;
const int mxn = 1e3 + 5;
bool R[mxn][mxn]; int sum[mxn * 2 + 5], n, m;
vpii adj(pii p){
int x = p.first, y = p.second; vpii ret;
if(x != 0)ret.pb(mp(x-1,y));
if(x != n-1)ret.pb(mp(x+1,y));
if(y != 0)ret.pb(mp(x,y-1));
if(y != m-1)ret.pb(mp(x,y+1));
return ret;
}
bool check(int x, int y){
if(x == 0 && y == 0)return 0; if(x == n-1 && y == m-1)return 0;
// dout2(x,y);
if((x == 0 || R[x-1][y]) && (y == 0 || R[x][y-1]))return 1;
if((x == n-1 || R[x+1][y]) && (y == m-1 || R[x][y+1]))return 1; return 0;
}
bool ins(int x, int y){
// vout(sum);
if(R[x][y])return 1;
if(sum[x+y] == 1)return 0;
queue<pii>q; q.push(mp(x,y)); sum[x+y]--; R[x][y] = 1;
while(!q.empty()){
auto [a,b] = q.front(); q.pop();
for(auto &[g,w] : adj(mp(a,b))){
if(R[g][w])continue;
if(check(g,w)){
R[g][w] = 1; sum[g+w]--; q.push(mp(g,w));
}
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
cin>>n>>m; f0r(i,n)f0r(j,m)sum[i+j]++; f0r(i,n)f0r(j,m){in(x); if(x == 1)ins(i,j);}
in(Q); f0r(i,Q){in2(x,y); x--; y--; out(ins(x,y));}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |