#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
int g[n+2][m+2] = {};
rep (i,1,n) {
rep (j,1,m) {
cin >> g[i][j], g[i][j]^=1;
}
}
rrep (i,n,1) {
rrep (j,m,1) {
bool b = 0;
if (i<n) b |= g[i+1][j];
if (j<m) b |= g[i][j+1];
if (i==n && j==m) b = 1;
g[i][j] &= b;
}
}
int Q; cin >> Q;
while (Q--) {
int x, y; cin >> x >> y;
int g2[n+2][m+2] = {};
memcpy(g2, g, sizeof(g));
queue<ii> q;
q.push({x,y});
while (len(q)) {
int i = q.front().fi, j = q.front().se;
q.pop();
if ((i<x || j<y) && (!g2[i][j] || g2[i+1][j] || g2[i][j+1])) continue;
g2[i][j] = 0;
if (i>1 && g2[i-1][j]) q.push({i-1,j});
if (j>1 && g2[i][j-1]) q.push({i,j-1});
}
if (!g2[1][1]) {
cout << "0\n";
}
else {
cout << "1\n";
q.push({x,y});
while (len(q)) {
int i = q.front().fi, j = q.front().se;
q.pop();
if ((i<x || j<y) && (!g2[i][j] || g2[i+1][j] || g2[i][j+1])) continue;
g[i][j] = 0;
if (i>1 && g[i-1][j]) q.push({i-1,j});
if (j>1 && g[i][j-1]) q.push({i,j-1});
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
6 ms |
356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
6 ms |
356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |