#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
vector<int> cnt(2050);
vector<vector<int>> v;
void dfs(int a,int b){
v[a][b]=1;
cnt[a+b]--;
if(v[a-1][b+1]){
if(v[a][b+1]==0){
dfs(a,b+1);
}
if(v[a-1][b]==0){
dfs(a-1,b);
}
}
if(v[a+1][b-1]){
if(v[a+1][b]==0){
dfs(a+1,b);
}
if(v[a][b-1]==0){
dfs(a,b-1);
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
v.resize(n+2,vector<int>(m+2,1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cnt[i+j]++;
v[i][j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int p;cin>>p;
if(p==1){
if(v[i][j]==0){
dfs(i,j);
}
}
}
}
int q;cin>>q;
for(int i=0;i<q;i++){
int a,b;cin>>a>>b;
if(v[a][b]==1){
cout<<1<<endl;
continue;
}
if(cnt[a+b]<=1){
cout<<0<<endl;
}
else{
cout<<1<<endl;
dfs(a,b);
}
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<v[i][j]<<' ';
}
cout<<endl;
}
*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
7 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
116 ms |
4716 KB |
Output is correct |
13 |
Correct |
46 ms |
3868 KB |
Output is correct |
14 |
Correct |
207 ms |
5532 KB |
Output is correct |
15 |
Correct |
206 ms |
5348 KB |
Output is correct |
16 |
Correct |
225 ms |
5836 KB |
Output is correct |
17 |
Correct |
235 ms |
6208 KB |
Output is correct |
18 |
Correct |
296 ms |
5992 KB |
Output is correct |
19 |
Correct |
239 ms |
6288 KB |
Output is correct |
20 |
Correct |
252 ms |
6312 KB |
Output is correct |
21 |
Correct |
242 ms |
6332 KB |
Output is correct |