#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1000;
int n, m, q, c[mxN][mxN], cnt[2*mxN];
bool can[2*mxN][mxN][2];
void dfs(int i, int j, bool t) {
can[i][j][t]=1;
if (t==0) {
if (i+1<n&&!c[i+1][j]) dfs(i+1, j, 0);
if (j+1<m&&!c[i][j+1]) dfs(i, j+1, 0);
} else {
if (i&&!c[i-1][j]) dfs(i-1, j, 1);
if (j&&!c[i][j-1]) dfs(i, j-1, 1);
}
}
void rem(int i, int j, bool t) {
cnt[i+j]-=can[i][j][0]&&can[i][j][1];
can[i][j][t]=0;
if (t==0) {
if (i+1<n&&can[i+1][j][0]&&(j==0||!can[i+1][j-1][0])) rem(i+1, j, 0);
if (j+1<m&&can[i][j+1][0]&&(i==0||!can[i-1][j+1][0])) rem(i, j+1, 0);
} else {
if (i&&can[i-1][j][1]&&(j==m-1||!can[i-1][j+1][1])) rem(i-1, j, 1);
if (j&&can[i][j-1][1]&&(i==n-1||!can[i+1][j-1][1])) rem(i, j-1, 1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
for (int j=0; j<m; ++j)
cin >> c[i][j];
dfs(0, 0, 0);
dfs(n-1, m-1, 1);
for (int i=0; i<n; ++i)
for (int j=0; j<m; ++j)
if (can[i][j][0]&&can[i][j][1])
++cnt[i+j];
cin >> q;
while(q--) {
int i, j;
cin >> i >> j, --i, --j;
if (cnt[i+j]>1||!can[i][j][0]||!can[i][j][1]) {
cout << "1\n";
if (can[i][j][0]&&can[i][j][1]) {
rem(i, j, 0);
rem(i, j, 1);
}
} else
cout << "0\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5067 ms |
724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5067 ms |
724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |