#include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> vacant;
vector<int> diag;
void block(int i, int j) {
if (i == 0 || j == 0 || i == vacant.size()-1 || j == vacant[i].size()-1) return;
diag[i+j-1]--;
vacant[i][j] = false;
// if the cell north-east to this one is also blocked, it makes no sense to keep the one above this and the one to the right of this vacant
if (!vacant[i-1][j+1]) {
if (vacant[i][j+1]) block(i, j+1);
if (vacant[i-1][j]) block(i-1, j);
}
// similarly, if the cell south-west to this one is also blocked, the one to the left of this one and the one below this one can also be blocked
if (!vacant[i+1][j-1]) {
if (vacant[i][j-1]) block(i, j-1);
if (vacant[i+1][j]) block(i+1, j);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n+2, vector<int>(m+2));
diag.resize(n+m+1);
vacant.resize(n+2, vector<bool>(m+2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
diag[i+j-1]++;
vacant[i][j] = true;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (grid[i][j] && vacant[i][j]) block(i, j);
}
}
int q;
cin >> q;
while (q--) {
int r, c;
cin >> r >> c;
if (!vacant[r][c]) cout << "1\n";
else if (diag[r+c-1] == 1) cout << "0\n";
else {
cout << "1\n";
block(r, c);
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |