#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int N = 1069;
int n, m, a[N][N], dp1[N][N], dp2[N][N];
int g[N][N], cnt[2 * N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cin >> a[i][j];
g[i][j] = a[i][j];
}
}
for (int i = 1;i <= m;i++)
g[0][i] = g[n + 1][i] = 1;
for (int i = 1;i <= n;i++)
g[i][0] = g[i][m + 1] = 1;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
if ((i == 1 && j == 1) || (i == n && j == m))
continue;
if (!g[i][j] && g[i][j - 1] && g[i - 1][j])
g[i][j] = 1;
}
}
for (int i = n;i >= 1;i--) {
for (int j = m;j >= 1;j--) {
if ((i == 1 && j == 1) || (i == n && j == m))
continue;
if (!g[i][j] && g[i][j + 1] && g[i + 1][j])
g[i][j] = 1;
}
}
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
if (!g[i][j])
cnt[i + j]++;
}
}
g[1][1] = g[n][m] = 0;
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
if (g[x][y])
cout << 1 << '\n';
else {
if (cnt[x + y] == 1)
cout << 0 << '\n';
else {
cout << 1 << '\n';
queue<pair<int, int>> q;
q.push({x, y});
while (!q.empty()) {
auto v = q.front();
int i = v.f, j = v.s;
g[i][j] = 1;
cnt[i + j]--;
q.pop();
if (i > 0 && g[i - 1][j + 1] && !g[i - 1][j])
q.push({i - 1, j});
if (j > 0 && g[i + 1][j - 1] && !g[i][j - 1])
q.push({i, j - 1});
}
q.push({x, y});
while (!q.empty()) {
auto v = q.front();
int i = v.f, j = v.s;
g[i][j] = 1;
if (i != x && j != y)
cnt[i + j]--;
q.pop();
if (i > 0 && g[i - 1][j + 1] && !g[i][j + 1])
q.push({i, j + 1});
if (j > 0 && g[i + 1][j - 1] && !g[i + 1][j])
q.push({i + 1, j});
}
}
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |