#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define MEM(x,a) memset((x),a,sizeof((x)))
#define F first
#define S second
#define imx INT_MAX
const long long MOD = (long long)(1e9+7);
const long long MMX = (long long)(1e18);
typedef long long LL;
typedef pair<int,int> pii;
int n,m,q,xi,xj,co[1005][1005][2],k,fur[1005][1005],mi,cp[1005][1005][2];
bool cc;
queue<pair<int,int> >qq;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&fur[i][j]);
}
}
co[1][1][0]=1;
co[n][m][1]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(fur[i][j]==1||co[i][j][0]==0)continue;
if(fur[i+1][j]==0)co[i+1][j][0]++;
if(fur[i][j+1]==0)co[i][j+1][0]++;
}
}
for(int i=n;i>0;i--)
{
for(int j=m;j>0;j--)
{
if(fur[i][j]==1||co[i][j][1]==0)continue;
if(fur[i-1][j]==0)co[i-1][j][1]++;
if(fur[i][j-1]==0)co[i][j-1][1]++;
}
}
scanf("%d",&q);
while(q--)
{
cc=false;
scanf("%d %d",&xi,&xj);
//for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cp[i][j][0]=0,cp[i][j][1]=0;
// cp[1][1][0]=1;
// cp[n][m][1]=1;
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=m;j++)
// {
// if(fur[i][j]==1||cp[i][j][0]==0)continue;
// if(fur[i+1][j]==0)cp[i+1][j][0]++;
// if(fur[i][j+1]==0)cp[i][j+1][0]++;
// }
// }
// for(int i=n;i>0;i--)
// {
// for(int j=m;j>0;j--)
// {
// if(fur[i][j]==1||cp[i][j][1]==0)continue;
// if(fur[i-1][j]==0)cp[i-1][j][1]++;
// if(fur[i][j-1]==0)cp[i][j-1][1]++;
// }
// }
// printf("-------------------\n");
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=m;j++)
// {
// //if(co[i][j][0]!=cp[i][j][0])printf("%d %d\n",i,j);
// //if(co[i][j][1]!=cp[i][j][1])printf("%d %d\n",i,j);
// printf("[%d %d] ",co[i][j][0],co[i][j][1]);
// }
// printf("\n");
// }
if(xi+xj<n)
{
if(co[xi][xj][1]>0)mi=1;
else mi=0;
for(int i=xi-1;i>0;i--)
{
if(fur[i][xj]==1)mi=0;
if(co[i][xj][0]>0&&co[i][xj][1]-mi>0)
{
cc=true;
//printf("%d %d\n",i,xj);
break;
}
}
if(co[xi][xj][1]>0)mi=1;
else mi=0;
if(!cc)
for(int j=xj-1;j>0;j--)
{
if(fur[xi][j]==1)mi=0;
if(co[xi][j][0]>0&&co[xi][j][1]-mi>0)
{
cc=true;
//printf("%d %d\n",xi,j);
break;
}
}
}
else
{
if(co[xi][xj][0]>0)mi=1;
else mi=0;
if(!cc)
for(int i=xi+1;i<=n;i++)
{
if(fur[i][xj]==1)mi=0;
if(co[i][xj][0]-mi>0&&co[i][xj][1]>0)
{
cc=true;
//printf("%d %d\n",i,xj);
break;
}
}
if(co[xi][xj][0]>0)mi=1;
else mi=0;
if(!cc)
for(int j=xj+1;j<=m;j++)
{
if(fur[xi][j]==1)mi=0;
if(co[xi][j][0]-mi>0&&co[xi][j][1]>0)
{
cc=true;
//printf("%d %d\n",xi,j);
break;
}
}
}
if(cc)
{
printf("1\n");
fur[xi][xj]=1;
if(co[xi][xj][0]>0)
{
co[xi][xj][0]=0;
qq.push({xi,xj});
while(!qq.empty())
{
if(qq.front().F+1<=n)
{
co[qq.front().F+1][qq.front().S][0]--;
if(co[qq.front().F+1][qq.front().S][0]==0)
{
qq.push({qq.front().F+1,qq.front().S});
}
}
if(qq.front().S+1<=m)
{
co[qq.front().F][qq.front().S+1][0]--;
if(co[qq.front().F][qq.front().S+1][0]==0)
{
qq.push({qq.front().F,qq.front().S+1});
}
}
qq.pop();
}
}
if(co[xi][xj][1]>0)
{
co[xi][xj][1]=0;
qq.push({xi,xj});
while(!qq.empty())
{
if(qq.front().F-1>0)
{
co[qq.front().F-1][qq.front().S][1]--;
if(co[qq.front().F-1][qq.front().S][1]==0)
{
qq.push({qq.front().F-1,qq.front().S});
}
}
if(qq.front().S-1>0)
{
co[qq.front().F][qq.front().S-1][1]--;
if(co[qq.front().F][qq.front().S-1][1]==0)
{
qq.push({qq.front().F,qq.front().S-1});
}
}
qq.pop();
}
}
}
else printf("0\n");
}
}
Compilation message
furniture.cpp: In function 'int main()':
furniture.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
furniture.cpp:27:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d",&fur[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
furniture.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
furniture.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d %d",&xi,&xj);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
3 ms |
1100 KB |
Output is correct |
3 |
Correct |
4 ms |
972 KB |
Output is correct |
4 |
Correct |
5 ms |
1100 KB |
Output is correct |
5 |
Correct |
5 ms |
1100 KB |
Output is correct |
6 |
Correct |
6 ms |
1100 KB |
Output is correct |
7 |
Correct |
6 ms |
1100 KB |
Output is correct |
8 |
Correct |
6 ms |
1100 KB |
Output is correct |
9 |
Correct |
7 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
3 ms |
1100 KB |
Output is correct |
3 |
Correct |
4 ms |
972 KB |
Output is correct |
4 |
Correct |
5 ms |
1100 KB |
Output is correct |
5 |
Correct |
5 ms |
1100 KB |
Output is correct |
6 |
Correct |
6 ms |
1100 KB |
Output is correct |
7 |
Correct |
6 ms |
1100 KB |
Output is correct |
8 |
Correct |
6 ms |
1100 KB |
Output is correct |
9 |
Correct |
7 ms |
1100 KB |
Output is correct |
10 |
Correct |
27 ms |
1068 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
889 ms |
12588 KB |
Output is correct |
13 |
Correct |
111 ms |
11600 KB |
Output is correct |
14 |
Correct |
2858 ms |
13044 KB |
Output is correct |
15 |
Correct |
2590 ms |
12972 KB |
Output is correct |
16 |
Correct |
3529 ms |
13936 KB |
Output is correct |
17 |
Correct |
4101 ms |
15000 KB |
Output is correct |
18 |
Correct |
1843 ms |
14568 KB |
Output is correct |
19 |
Correct |
2990 ms |
15124 KB |
Output is correct |
20 |
Correct |
4038 ms |
15088 KB |
Output is correct |
21 |
Correct |
2125 ms |
15028 KB |
Output is correct |