This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <queue>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 1000;
int v[5 + nmax][5 + nmax];
int init[5 + nmax][5 + nmax];
int blocks[5 + nmax * 2];
int n, m;
int solve(int x, int y) {
std::queue<std::pair<int,int>> q;
std::vector<std::pair<int,int>> judge;
if(v[x][y] == 1)
return 1;
if(blocks[x + y] == 1)
return 0;
q.push({x, y});
judge.push_back({x, y});
v[x][y] = 1;
blocks[x + y]--;
int verdict = 1;
while(0 < q.size()) {
x = q.front().first;
y = q.front().second;
q.pop();
if(v[x - 1][y + 1] == 1) {
if(v[x][y + 1] == 0) {
q.push({x, y + 1});
judge.push_back({x, y + 1});
v[x][y + 1] = 1;
blocks[x + y + 1]--;
if(blocks[x + y + 1] == 0) {
verdict = 0;
break;
}
}
if(v[x - 1][y] == 0) {
q.push({x - 1, y});
judge.push_back({x - 1, y});
v[x - 1][y] = 1;
blocks[x - 1 + y]--;
if(blocks[x - 1 + y] == 0) {
verdict = 0;
break;
}
}
}
if(v[x + 1][y - 1] == 1) {
if(v[x][y - 1] == 0) {
q.push({x, y - 1});
judge.push_back({x, y - 1});
v[x][y - 1] = 1;
blocks[x + y - 1]--;
if(blocks[x + y - 1] == 0) {
verdict = 0;
break;
}
}
if(v[x + 1][y] == 0) {
q.push({x + 1, y});
judge.push_back({x + 1, y});
v[x + 1][y] = 1;
blocks[x + 1 + y]--;
if(blocks[x + 1 + y] == 0) {
verdict = 0;
break;
}
}
}
}
/*
std::cout << "After\n";
for(int i = 1;i <= n; i++) {
for(int j = 1;j <= m; j++)
std::cout << v[i][j] << " ";
std::cout << '\n';
}
std::cout << '\n';
*/
if(verdict == 0) {
for(int i = 0; i < judge.size(); i++) {
int x = judge[i].first;
int y = judge[i].second;
v[x][y] = 0;
blocks[x + y]++;
}
return 0;
} else
return 1;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cin >> n >> m;
for(int i = 0;i <= n + 1; i++)
v[i][0] = v[i][m + 1] = 1;
for(int i = 0;i <= m + 1; i++)
v[0][i] = v[n + 1][i] = 1;
for(int i = 1;i <= n; i++)
for(int j = 1;j <= m; j++) {
std::cin >> init[i][j];
blocks[i + j]++;
}
for(int i = 1;i <= n; i++)
for(int j = 1;j <= m; j++)
if(init[i][j] == 1)
solve(i, j);
int q;
std::cin >> q;
for(int i = 1;i <= q; i++) {
int x, y;
std::cin >> x >> y;
std::cout << solve(x, y) << '\n';
}
return 0;
}
Compilation message (stderr)
furniture.cpp: In function 'int solve(int, int)':
furniture.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < judge.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |