#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
int n, m;
namespace sub1{
void solve(){
vector<vector<bool>>c(n, vector<bool>(m, false));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
int x;
cin >> x;
if(x == 1){
c[i][j] = true;
}
}
}
int q;
cin >> q;
for(int _ = 0; _ < q; _++){
int x, y;
cin >> x >> y;
c[--x][--y] = true;
vector<vector<bool>>vis(n, vector<bool>(m, false));
auto dfs = [&] (auto&& self, int x, int y){
if(x < 0 || x == n || y < 0 || y == m || vis[x][y] || c[x][y]){
return;
}
vis[x][y] = true;
self(self, x + 1, y);
self(self, x, y + 1);
};
dfs(dfs, 0, 0);
cout << vis[n - 1][m - 1] << "\n";
if(!vis[n - 1][m - 1]){
c[x][y] = false;
}
}
}
}
namespace sub2{
void solve(){
vector<vector<bool>>c(n, vector<bool>(m, false));
vector<vector<pair<int, int>>>point(n, vector<pair<int, int>>(m));
auto move = [&] (int i, int j){
c[i][j] = true;
queue<pair<int, int>>q;
q.emplace(i - 1, j);
q.emplace(i, j - 1);
while(!q.empty()){
auto [x, y] = q.front();
q.pop();
if(x < 0 || y < 0 || c[x][y]){
continue;
}
bool right = (y + 1 < m && !c[x][y + 1]), down = (x + 1 < n && !c[x + 1][y]);
if(right && down && point[x + 1][y] != point[x][y + 1]){
continue;
}
if(right && down){
point[x][y] = point[x][y + 1];
}
else if(right){
point[x][y] = make_pair(x, y + 1);
}
else if(down){
point[x][y] = make_pair(x + 1, y);
}
else{
c[x][y] = true;
}
q.emplace(x - 1, y);
q.emplace(x, y - 1);
}
};
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
point[i][j] = make_pair(i, j);
}
}
for(int i = 0; i + 1 < n; i++){
point[i][m - 1] = make_pair(i + 1, m - 1);
}
for(int j = 0; j + 1 < m; j++){
point[n - 1][j] = make_pair(n - 1, j + 1);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
int x;
cin >> x;
if(x == 1){
move(i, j);
}
}
}
// for(int i = 0; i < n; i++){
// for(int j = 0; j < m; j++){
// cout << c[i][j] << " ";
// }
// cout << endl;
// }
int q;
cin >> q;
for(int _ = 0; _ < q; _++){
int x, y;
cin >> x >> y;
if(c[--x][--y]){
cout << "1\n";
continue;
}
bool flag = true;
int i = 0, j = 0;
while(true){
auto [I, J] = point[i][j];
if(I == i && J == j){
break;
}
i = I;
if((j = J) == y && i == x){
flag = false;
break;
}
}
if(flag){
cout << "1\n";
move(x, y);
}
else{
cout << "0\n";
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> m;
if(n <= 100 && m <= 100){
sub2::solve();
}
else{
sub2::solve();
}
}
Compilation message (stderr)
furniture.cpp: In function 'int main()':
furniture.cpp:136:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |