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 <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define mp make_pair
#define ar array
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define trav(i, a) for (auto& i : a)
template <class T> istream &operator>>(istream& in, vector<T> &v) {trav(i, v) in >> i; return in;}
#ifdef LOCAL
#include "../../lib/debug.h"
#else
#define dbg(...)
#endif
const int N = 1000 + 5;
int a[N][N]; // maintain whether we can reach end
int d[2 * N];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;
void upd(int x, int y) {
d[x + y]--;
a[x][y] = 1;
FOR(i, 0, 4) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!(nx <= 0 or nx > n or ny <= 0 or ny > m) and !a[nx][ny] and
(a[nx + 1][ny] and a[nx][ny + 1] and mp(nx, ny) != mp(n, m) or a[nx - 1][ny] and a[nx][ny - 1] and mp(nx, ny) != mp(1ll, 1ll))) {
upd(nx, ny);
}
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
FOR(i, 0, n + 2) FOR(j, 0, m + 2) a[i][j] = 1;
FOR(i, 1, n + 1) FOR(j, 1, m + 1) {
a[i][j] = 0;
d[i + j]++;
}
FOR(i, 1, n + 1) FOR(j, 1, m + 1) {
int x; cin >> x;
if (x) upd(i, j);
}
// FOR(i, 1, n + 1) {FOR(j, 1, m + 1) cout << a[i][j] << " "; cout << endl;}
int q; cin >> q; while (q--) {
int i, j; cin >> i >> j;
if (a[i][j] == 1) cout << 1 << endl;
else if (d[i + j] == 1) cout << 0 << endl;
else cout << 1 << endl, upd(i, j);
}
}
Compilation message (stderr)
furniture.cpp: In function 'void upd(long long int, long long int)':
furniture.cpp:39:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | (a[nx + 1][ny] and a[nx][ny + 1] and mp(nx, ny) != mp(n, m) or a[nx - 1][ny] and a[nx][ny - 1] and mp(nx, ny) != mp(1ll, 1ll))) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |