#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
int n,m;
vector<vector<int>> v;
vector<int> freq;
bool valid(int i,int j){
return min(i,j)>=1 && i<=n && j<=m;
}
void upd(int i,int j){
if(!valid(i,j)) return;
if(v[i][j]==0) return;
freq[i+j]++;
if(valid(i+1,j) && v[i+1][j]==0){
if(!valid(i+1,j-1) || v[i+1][j-1]==1){
v[i+1][j]=1;
upd(i+1,j);
}
}
if(valid(i,j+1) && v[i][j+1]==0){
if(!valid(i-1,j+1) || v[i-1][j+1]==1){
v[i][j+1]=1;
upd(i,j+1);
}
}
if(valid(i-1,j) && v[i-1][j]==0){
if(!valid(i-1,j+1) || v[i-1][j+1]==1){
v[i-1][j]=1;
upd(i-1,j);
}
}
if(valid(i,j-1) && v[i][j-1]==0){
if(!valid(i+1,j-1) || v[i+1][j-1]==1){
v[i][j-1]=1;
upd(i,j-1);
}
}
}
void solve(){
cin >> n >> m;
v.assign(n+5,vector<int>(m+5,0));
freq.assign(n+m+5,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int u;
cin >> u;
if(u==1){
v[i][j]=1;
upd(i,j);
}
}
}
int q;
cin >> q;
while(q--){
int a,b;
cin >> a >> b;
if(v[a][b]==1){
cout << 1 << endl;
continue;
}
int u = a + b;
if(freq[u]==min(a-1,m-b)+min(n-a,b-1)){
cout << 0 << endl;
continue;
}
cout << 1 << endl;
v[a][b]=1;
upd(a,b);
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |