#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=1007;
int ile[2*N];
bool a[N][N];
bool odw[N][N];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
bool good(int i,int j)
{
return min(i,j)>0&&i<=n&&j<=m;
}
bool check(int i,int j)
{
bool is=1,is1=1;
if(i==n&&j==m||i==1&&j==1) return 0;
if(good(i-1,j)&&!odw[i-1][j]) is=0;
if(good(i,j-1)&&!odw[i][j-1]) is=0;
if(good(i+1,j)&&!odw[i+1][j]) is1=0;
if(good(i,j+1)&&!odw[i][j+1]) is1=0;
return is|is1;
}
void dfs(int i,int j)
{
odw[i][j]=1;
ile[i+j]--;
for(int l=0;l<4;l++)
{
int ni=i+dx[l],nj=j+dy[l];
if(good(ni,nj)&&!odw[ni][nj]&&check(ni,nj)) dfs(ni,nj);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ile[i+j]++;
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]) dfs(i,j);
}
}
int q;
cin>>q;
while(q--)
{
int x,y;
cin>>x>>y;
if(!odw[x][y]&&ile[x+y]==1) cout<<0<<endl;
else
{
a[x][y]=1;
if(!odw[x][y]) dfs(x,y);
cout<<1<<endl;
}
}
return 0;
}
Compilation message
furniture.cpp: In function 'bool check(int, int)':
furniture.cpp:34:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
34 | if(i==n&&j==m||i==1&&j==1) return 0;
| ~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |