#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;
}
stack<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.top().first;
int y=stk.top().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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
4 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 |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
4 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 |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
11 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
240 ms |
4856 KB |
Output is correct |
13 |
Correct |
87 ms |
4216 KB |
Output is correct |
14 |
Correct |
352 ms |
5624 KB |
Output is correct |
15 |
Correct |
351 ms |
5880 KB |
Output is correct |
16 |
Correct |
345 ms |
6008 KB |
Output is correct |
17 |
Correct |
363 ms |
6264 KB |
Output is correct |
18 |
Correct |
374 ms |
6136 KB |
Output is correct |
19 |
Correct |
373 ms |
6392 KB |
Output is correct |
20 |
Correct |
317 ms |
6392 KB |
Output is correct |
21 |
Correct |
380 ms |
6392 KB |
Output is correct |