This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
const int MX = 1005;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, q, a[MX][MX], diag[MX + MX];
void dfs(int i, int j){
a[i][j] = 1; diag[i + j] --;
for(int ni, nj, d = 0; d < 4; d ++){
ni = i + dx[d];
nj = j + dy[d];
if(!(ni == 1 && nj == 1) && !(ni == n && nj == m) && !a[ni][nj])
if((a[ni - 1][nj] && a[ni][nj - 1]) || (a[ni + 1][nj] && a[ni][nj + 1]))
dfs(ni, nj);
}
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> m;
for(int i = 0; i <= n + 1; i ++)
for(int j = 0; j <= m + 1; j ++){
if(i == 0 || j == 0 || i == n + 1 || j == m + 1) a[i][j] = 1;
else diag[i + j] ++;
}
for(int x, i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++){
cin >> x;
if(x && !a[i][j]) dfs(i, j);
}
cin >> q;
for(int x, y, i = 0; i < q; i ++){
cin >> x >> y;
if(a[x][y]) cout << "1\n";
else if(diag[x + y] > 1 && !a[x][y]){
dfs(x, y); cout << "1\n";
}else cout << "0\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |