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 <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 1005;
int n, m, q, g[N][N], s[2 * N];
void gogo(int x, int y);
void go(int x, int y){
if((x == 1 && y == 1) || (x == n && y == m)) return;
if(g[x][y] == 1) return;
if((g[x - 1][y] && g[x][y - 1]) || (g[x + 1][y] && g[x][y + 1])){
gogo(x, y);
}
}
void gogo(int x, int y){
if(g[x][y] == 1) return;
g[x][y] = 1;
s[x + y]--;
go(x - 1, y);
go(x, y - 1);
go(x + 1, y);
go(x, y + 1);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int j = 0 ; j <= m + 1 ; j++){
g[0][j] = g[n + 1][j] = 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++){
s[i + j]++;
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
int x;
cin >> x;
if(x) gogo(i, j);
}
}
cin >> q;
while(q--){
int x, y;
cin >> x >> y;
if(g[x][y]) cout << "1\n";
else if(s[x + y] == 1) cout << "0\n";
else{
cout << "1\n";
gogo(x, y);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |