//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 |
Runtime error |
303 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
303 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |