#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];
void Go(int x, int y) {
g[x][y] = 1;
cnt[x + y]--;
if (g[x + 1][y - 1]) {
if (!g[x][y - 1])
Go(x, y - 1);
if (!g[x + 1][y])
Go(x + 1, y);
}
if (g[x - 1][y + 1]) {
if (!g[x - 1][y])
Go(x - 1, y);
if (!g[x][y + 1])
Go(x, y + 1);
}
}
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 = 0;i <= m + 1;i++)
g[0][i] = g[n + 1][i] = 1;
for (int i = 0;i <= n + 1;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 <= m;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';
Go(x, y);
}
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4580 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
3 ms |
4696 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4580 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
3 ms |
4696 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
7 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
102 ms |
21588 KB |
Output is correct |
13 |
Correct |
43 ms |
18768 KB |
Output is correct |
14 |
Correct |
192 ms |
26456 KB |
Output is correct |
15 |
Correct |
183 ms |
26848 KB |
Output is correct |
16 |
Correct |
196 ms |
27716 KB |
Output is correct |
17 |
Correct |
269 ms |
28296 KB |
Output is correct |
18 |
Correct |
236 ms |
27872 KB |
Output is correct |
19 |
Correct |
246 ms |
30844 KB |
Output is correct |
20 |
Correct |
209 ms |
30920 KB |
Output is correct |
21 |
Correct |
207 ms |
30844 KB |
Output is correct |