#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long, long long>
int n,m,q,diagonal[2069];
bool grid[1069][1069];
bool inrange(int x1, int y1)
{
return x1 >= 1 and x1 <= n and y1 >= 1 and y1 <= m;
}
void dfs(int x, int y)
{
if((x == 1 and y == 1) or (x == n and y == m)) return;
// cout<<"asu : "<<x<<" "<<y<<'\n';
diagonal[x+y] -= !grid[x][y];
grid[x][y] = 1;
// grid[i][j] = 1 -> gk bisa dilewatin
// jika grid[i][j] = 1, maka :
// 1. grid[i+1][j] dan grid[i][j-1] bisa jadi 1 asalkan grid[i-1][j+1] = 1
// 2. grid[i-1][j] dan grid[i][j+1] bisa jadi 1 asalkan grid[i+1][j-1] = 1
if(grid[x-1][y+1])
{
if(!grid[x-1][y])
{
dfs(x-1,y);
}
if(!grid[x][y+1])
{
dfs(x,y+1);
}
}
if(grid[x+1][y-1])
{
if(!grid[x][y-1])
{
dfs(x,y-1);
}
if(!grid[x+1][y])
{
dfs(x+1,y);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int i,j;
cin>>n>>m;
// memset(grid,1,sizeof(grid));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
++diagonal[i+j];
grid[n+1][j] = 1;
grid[i][m+1] = 1;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>grid[i][j];
diagonal[i+j] -= grid[i][j];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(grid[i][j])
{
dfs(i,j);
}
}
}
// cout<<"debug : \n";
// for(i=1;i<=n+1;i++)
// {
// for(j=1;j<=m+1;j++)
// {
// cout<<grid[i][j]<<(j == m+1 ? '\n':' ');
// }
// }
// cout<<"query : \n";
cin>>q;
while(q--)
{
int x,y;
cin>>x>>y;
if(grid[x][y])
{
cout<<1<<'\n';
continue;
}
if(diagonal[x+y] == 1)
{
cout<<0<<'\n';
continue;
}
cout<<1<<'\n';
dfs(x,y);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |