This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define N 1005
typedef long long ll;
using namespace std;
bool r[N][N];
int c[N][N];
int a[N*2];
vector<pair<int,int> >v;
void detect(int x,int y){
r[x][y]=1;
a[x+y]--;
if(r[x+1][y-1]==1){
if(r[x][y-1]==0){
detect(x,y-1);
}
if(r[x+1][y]==0){
detect(x+1,y);
}
}
if(r[x-1][y+1]==1){
if(r[x-1][y]==0){
detect(x-1,y);
}
if(r[x][y+1]==0){
detect(x,y+1);
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> c[i][j];
if(c[i][j]==1) v.pb({i,j});
a[i+j]++;
}
}
for(int i=0;i<=n+1;i++){
r[i][0]=1;
r[i][m+1]=1;
}
for(int i=0;i<=m+1;i++){
r[0][i]=1;
r[n+1][i]=1;
}
for(pair<int,int>fur:v){
if(r[fur.ff][fur.ss]==0) detect(fur.ff,fur.ss);
}
int q;
cin >> q;
while(q--){
int x,y;
cin >> x >> y;
if(r[x][y]==1){
cout << 1 << '\n';
}
else{
if(a[x+y]>1){
cout << 1 << '\n';
detect(x,y);
}
else cout << 0 << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |