Submission #936391

#TimeUsernameProblemLanguageResultExecution timeMemory
936391OAleksaFurniture (JOI20_furniture)C++14
100 / 100
269 ms30920 KiB
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int N = 1069;
int n, m, a[N][N], dp1[N][N], dp2[N][N];
int g[N][N], cnt[2 * N];
void Go(int x, int y) {
	g[x][y] = 1;
	cnt[x + y]--;
	if (g[x + 1][y - 1]) {
		if (!g[x][y - 1])
			Go(x, y - 1);
		if (!g[x + 1][y])
			Go(x + 1, y);
	}
	if (g[x - 1][y + 1]) {
		if (!g[x - 1][y])
			Go(x - 1, y);
		if (!g[x][y + 1])
			Go(x, y + 1);
	}
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n >> m;
  	for (int i = 1;i <= n;i++) {
  		for (int j = 1;j <= m;j++) {
  			cin >> a[i][j];
  			g[i][j] = a[i][j];
  		}
  	}
  	for (int i = 0;i <= m + 1;i++) 
  		g[0][i] = g[n + 1][i] = 1;
  	for (int i = 0;i <= n + 1;i++)
  		g[i][0] = g[i][m + 1] = 1;
  	for (int i = 1;i <= n;i++) {
  		for (int j = 1;j <= m;j++) {
  			if ((i == 1 && j == 1) || (i == n && j == m))
  				continue;
  			if (!g[i][j] && g[i][j - 1] && g[i - 1][j])
  				g[i][j] = 1;
  		}
  	}
  	for (int i = n;i >= 1;i--) {
  		for (int j = m;j >= 1;j--) {
  			if ((i == 1 && j == 1) || (i == n && j == m))
  				continue;
  			if (!g[i][j] && g[i][j + 1] && g[i + 1][j])
  				g[i][j] = 1;
  		}
  	}
  	for (int i = 1;i <= n;i++) {
  		for (int j = 1;j <= m;j++) {
  			if (!g[i][j])
  				cnt[i + j]++;
  		}
  	}
  	g[1][1] = g[n][m] = 0;
  	int q;
  	cin >> q;
  	while (q--) {
  		int x, y;
  		cin >> x >> y;
  		if (g[x][y]) 
  			cout << 1 << '\n';
  		else {
  			if (cnt[x + y] == 1)
  				cout << 0 << '\n';
  			else {
  				cout << 1 << '\n';
  				Go(x, y);
	  		}
  		}
  	}
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...