Submission #936336

# Submission time Handle Problem Language Result Execution time Memory
936336 2024-03-01T15:53:27 Z OAleksa Furniture (JOI20_furniture) C++14
0 / 100
2 ms 7260 KB
#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];
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];
  	}
  	for (int i = 1;i <= n;i++) {
  		for (int j = 1;j <= m;j++) 
  			dp1[i][j] = (dp1[i - 1][j] | dp1[i][j - 1] | a[i][j]);
  	}
  	for (int i = n;i >= 0;i--) {
  		for (int j = m;j >= 0;j--)
  			dp2[i][j] = (dp2[i + 1][j] | dp2[i][j + 1] | a[i][j]);
  	}
  	for (int i = 1;i <= n;i++) {
  		for (int j = 1;j <= m;j++)
  			g[i][j] = (dp1[i][j] | dp2[i][j]);
  	}
  	for (int i = 1;i <= m;i++) 
  		g[0][i] = g[n + 1][i] = 1;
  	for (int i = 1;i <= n;i++)
  		g[i][0] = g[i][m + 1] = 1;
  	g[1][1] = g[n][m] = 0;
  	int q;
  	cin >> q;
  	while (q--) {
  		int x, y;
  		cin >> x >> y;
  		int ok = 0;
  		queue<pair<int, int>> q;
			q.push({x, y});
			while (!q.empty()) {
				auto v = q.front();
				int i = v.f, j = v.s;
				g[i][j] = 1;
				q.pop();
				if (i > 0 && j <= m && g[i - 1][j + 1] && !g[i - 1][j])
					q.push({i - 1, j});
				if (i <= n && j > 0 && g[i + 1][j - 1] && !g[i][j - 1])
					q.push({i, j - 1});
			}
			q.push({x, y});
			while (!q.empty()) {
				auto v = q.front();
				int i = v.f, j = v.s;
				g[i][j] = 1;
				q.pop();
				if (i > 0 && j <= m && g[i - 1][j + 1] && !g[i][j + 1])
					q.push({i, j + 1});
				if (i <= n && j > 0 && g[i + 1][j - 1] && !g[i + 1][j])
					q.push({i + 1, j});
			}
  		for (int i = x;i >= 1;i--)
  			ok |= (g[i][y] == 0);
  		for (int i = y;i >= 1;i--)
  			ok |= (g[x][i] == 0);
  		if (ok) 
  			cout << 1 << '\n';
  		else {
  			q.push({x, y});
				while (!q.empty()) {
					auto v = q.front();
					int i = v.f, j = v.s;
					g[i][j] = 0;
					q.pop();
					if (i > 0 && j <= m && g[i - 1][j + 1] && g[i - 1][j])
						q.push({i - 1, j});
					if (i <= n && j > 0 && g[i + 1][j - 1] && g[i][j - 1])
						q.push({i, j - 1});
				}
				q.push({x, y});
				while (!q.empty()) {
					auto v = q.front();
					int i = v.f, j = v.s;
					g[i][j] = 0;
					q.pop();
					if (i > 0 && j <= m && g[i - 1][j + 1] && g[i][j + 1])
						q.push({i, j + 1});
					if (i <= n && j > 0 && g[i + 1][j - 1] && g[i + 1][j])
						q.push({i + 1, j});
				}
  			cout << 0 << '\n';
  		}
  	}
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 7260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 7260 KB Output isn't correct
2 Halted 0 ms 0 KB -