//Challenge: Accepted
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int a[maxn][maxn], cnt[2*maxn];
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool vis[maxn][maxn], vis2[maxn][maxn];
int n, m;
bool in(int x, int y){return x >= 0 && x < n && y >= 0 && y < m;}
bool good(int x, int y){return in(x, y) && vis[x][y];}
void dfs(int x, int y) {
vis[x][y] = 0;
cnt[x+y]--;
for (int k = 0;k < 4;k++) {
int nx = x + dir[k][0], ny = y + dir[k][1];
if (in(nx, ny) && vis[nx][ny] && !(nx == 0 && ny == 0) && !(nx == n - 1 && ny == m - 1)) {
if (!(good(nx + 1, ny) || good(nx, ny+1)) || !(good(nx-1, ny) || good(nx, ny-1))) {
dfs(nx, ny);
}
}
}
}
int main() {
io
cin >> n >> m;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> a[i][j];
}
}
queue<pii> que;
que.push({0, 0});
vis[0][0] = 1;
auto add = [&] (int x, int y){
if (in(x, y) && !vis[x][y] && !a[x][y]) {
que.push({x, y});
vis[x][y] = 1;
}
};
while (que.size()) {
pii cur = que.front();que.pop();
add(cur.ff + 1, cur.ss);
add(cur.ff, cur.ss+1);
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
vis2[i][j] = vis[i][j];
vis[i][j] = 0;
}
}
que.push({n-1, m-1});
vis[n-1][m-1] = 1;
while (que.size()) {
pii cur = que.front();que.pop();
add(cur.ff - 1, cur.ss);
add(cur.ff, cur.ss-1);
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
vis[i][j] &= vis2[i][j];
if (vis[i][j]) cnt[i+j]++;
}
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y; x--, y--;
if (vis[x][y] && cnt[x+y] == 1) {
cout << 0 << "\n";
} else {
if (vis[x][y]) dfs(x, y);
cout << 1 << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
980 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
980 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
154 ms |
10848 KB |
Output is correct |
13 |
Correct |
66 ms |
7716 KB |
Output is correct |
14 |
Correct |
242 ms |
15368 KB |
Output is correct |
15 |
Correct |
270 ms |
15572 KB |
Output is correct |
16 |
Correct |
256 ms |
16616 KB |
Output is correct |
17 |
Correct |
282 ms |
17444 KB |
Output is correct |
18 |
Correct |
269 ms |
16920 KB |
Output is correct |
19 |
Correct |
289 ms |
17804 KB |
Output is correct |
20 |
Correct |
297 ms |
17840 KB |
Output is correct |
21 |
Correct |
302 ms |
17996 KB |
Output is correct |