#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const ll mod = 1e9+7;
const int maxn = 1010;
int n, m;
int g[maxn][maxn];
int diag[maxn+maxn];
int upd(int x, int y) {
// path via (x,y) is already blocked
if (g[x][y]==1) {
return 1;
}
// all paths go through (x,y)
if (diag[x+y]==1) {
return 0;
}
queue<pair<int,int>> stk;
auto proc = [&](int x, int y) {
if (g[x][y]==0) {
g[x][y]=1;
diag[x+y]--;
stk.emplace(x,y);
}
};
proc(x,y);
while (stk.size()) {
int x=stk.front().first;
int y=stk.front().second;
stk.pop();
// if neighboring diagonals are blocked, then some cells will also be blocked
if (g[x-1][y+1]==1) {
proc(x-1,y);
proc(x,y+1);
}
if (g[x+1][y-1]==1) {
proc(x,y-1);
proc(x+1,y);
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=0; i<=n+1; i++) {
for (int j=0; j<=m+1; j++) {
g[i][j]=1;
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
g[i][j]=0;
diag[i+j]++;
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
int x;
cin>>x;
if (x==1) {
upd(i,j);
}
}
}
int q;
cin>>q;
while (q--) {
int x, y;
cin>>x>>y;
cout<<upd(x,y)<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
2 ms |
768 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
2 ms |
768 KB |
Output is correct |
4 |
Correct |
3 ms |
768 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
8 |
Correct |
4 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
768 KB |
Output is correct |
10 |
Correct |
12 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
544 KB |
Output is correct |
12 |
Correct |
210 ms |
4972 KB |
Output is correct |
13 |
Correct |
90 ms |
4184 KB |
Output is correct |
14 |
Correct |
347 ms |
5728 KB |
Output is correct |
15 |
Correct |
366 ms |
5896 KB |
Output is correct |
16 |
Correct |
339 ms |
6008 KB |
Output is correct |
17 |
Correct |
360 ms |
6392 KB |
Output is correct |
18 |
Correct |
357 ms |
6136 KB |
Output is correct |
19 |
Correct |
394 ms |
6392 KB |
Output is correct |
20 |
Correct |
327 ms |
6268 KB |
Output is correct |
21 |
Correct |
388 ms |
6340 KB |
Output is correct |