#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e9
#define eps 1e-7
#define eps1 1e-25
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
#define V 450
using namespace std;
const int MOD = 1e9+7;
const int nax = 1000005;
short grid[1005][1005],vis[1005][1005];
int cnt[nax];
int n,m;
int mov[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
int dfs(int x, int y){
if(x == n && y == m) return 0;
if(x > n || y > m) return 1;
if(grid[x][y]==1) return 1;
if(vis[x][y]) return grid[x][y];
vis[x][y] = 1;
grid[x][y] = min(dfs(x+1,y),dfs(x,y+1));
cnt[x+y]+=(grid[x][y]==0);
return grid[x][y];
}
void upd(int x, int y){
for (int i = 0; i < 4; ++i)
{
int nx = x + mov[i][0], ny = y + mov[i][1];
if(nx < 1 || ny < 1 || nx >n || ny > m ||grid[nx][ny] == 1||(nx == 1 && ny == 1)||(nx == n && ny == m)) continue;
if(grid[nx-1][ny] == 1 && grid[nx][ny-1] == 1){
grid[nx][ny] = 1;
}
if(grid[nx+1][ny] == 1 && grid[nx][ny+1] == 1){
grid[nx][ny] = 1;
}
if(grid[nx][ny] == 1){
cnt[nx+ny]--;
upd(nx,ny);
}
}
}
int main(){
optimise;
cin>>n >>m;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> grid[i+1][j+1];
if(grid[i+1][j+1] == 0) grid[i+1][j+1] = 2;
}
}
dfs(1,1);
grid[n][m] = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if(grid[i][j]==2)
grid[i][j] = 1;
}
}
int q;
cin >>q;
while(q--){
int x,y;
cin >> x >> y;
if(grid[x][y] == 1){
cout << 1 << endl;
continue;
}
else if(cnt[x+y] <= 1){
cout << 0 << endl;
continue;
}
cout << 1 << endl;
grid[x][y] = 1;
cnt[x+y]--;
upd(x,y);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
580 KB |
Output is correct |
2 |
Correct |
7 ms |
716 KB |
Output is correct |
3 |
Incorrect |
6 ms |
716 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
580 KB |
Output is correct |
2 |
Correct |
7 ms |
716 KB |
Output is correct |
3 |
Incorrect |
6 ms |
716 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |