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 int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1e3+5;
int a[MAX][MAX];
int dp[MAX][MAX]; // czy istnieje sciezka przechodzaca przez punkt, 0 jesli tak 1 jesli nie
int n,m;
int ile[MAX+MAX];
bool check(int x,int y){
if (ile[x+y]==1 && dp[x][y]==0)return false;
if (dp[x][y]==1)return false;
return true;
}
void zaznacz(int x,int y){
assert(check(x,y));
ile[x+y]--;
// cout<<"CELL "<<x<<" "<<y<<" "<<ile[x+y]<<" "<<x+y<<"\n";
a[x][y]=1;
dp[x][y]=1;
}
bool check2(int x,int y){
return x>=1 && x<=n && y>=1 && y<=m;
}
bool blocked(int x,int y){
if (dp[x][y]==1)return false;
if (dp[x-1][y]==1 && dp[x][y-1]==1 && (x>1 || y>1))return true;
if (dp[x+1][y]==1 && dp[x][y+1]==1 && (x<n || y<m))return true;
return false;
}
int px[4]={1,-1,0,0};
int py[4]={0,0,-1,1};
void block(int x,int y){
assert(check(x,y));
queue<pii>q;
zaznacz(x,y);
q.push(mp(x,y));
while (!q.empty()){
x=q.front().st;
y=q.front().nd;
q.pop();
for (int i=0;i<4;i++){
int tox=x+px[i];
int toy=y+py[i];
if (!check2(tox,toy))continue;
if (blocked(tox,toy)){
assert(tox>1 || toy>1);
zaznacz(tox,toy);
q.push(mp(tox,toy));
}
}
}
}
int32_t main(){
BOOST;
cin>>n>>m;
for (int i=0;i<=MAX-1;i++)for (int j=0;j<=MAX-1;j++){
dp[i][j]=1;
}
for (int i=1;i<=n;i++)for (int j=1;j<=m;j++)dp[i][j]=0,ile[i+j]++;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (a[i][j]==1){
if (check(i,j))
block(i,j);
}
}
}
int t;
cin>>t;
for (int z=0;z<t;z++){
int x,y;
cin>>x>>y;
if (check(x,y)){
block(x,y);
cout<<"1\n";
}
else cout<<"0\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |