Submission #296119

# Submission time Handle Problem Language Result Execution time Memory
296119 2020-09-10T09:51:30 Z Berted Furniture (JOI20_furniture) C++14
100 / 100
395 ms 12260 KB
#include <iostream>
#include <queue>
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;

const int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

int n, m, Q, ar[1001][1001];
int cnt[2001];
bool tr[1001][1001], bt[1001][1001];

queue<pii> q;
vector<pii> backtrack;

inline bool inGrid(int y, int x) {return 0 <= y && y < n && 0 <= x && x < m;}

inline void clear()
{
	for (auto &x : backtrack) {bt[x.fst][x.snd] = 0;}
	backtrack.clear();
}

inline void BFS(int y, int x)
{
	q.push({y, x}); backtrack.push_back({y, x});
	bt[y][x] = 1; tr[y][x] = 0;
	while (q.size())
	{
		int cntt[2] = {};
		y = q.front().fst, x = q.front().snd; q.pop();
		if (y == 0 && x == 0) continue;
		if (y == n - 1 && x == m - 1) continue;
		//cout << "REBFS " << y << " " << x << "\n";
		for (int i = 0; i < 4; i++)
		{
			if (inGrid(y + dir[i][0], x + dir[i][1]))
			{
				cntt[i / 2] += tr[y + dir[i][0]][x + dir[i][1]];
			}
		}
		if (tr[y][x] && cntt[0] && cntt[1]) {continue;}
		else
		{
			tr[y][x] = 0; cnt[y + x]--;
			for (int i = 0; i < 4; i++)
			{
				if (inGrid(y + dir[i][0], x + dir[i][1]))
				{
					if (!bt[y + dir[i][0]][x + dir[i][1]] && tr[y + dir[i][0]][x + dir[i][1]])
					{
						bt[y + dir[i][0]][x + dir[i][1]] = 1;
						backtrack.push_back({y + dir[i][0], x + dir[i][1]});
						q.push({y + dir[i][0], x + dir[i][1]});
					}
				}
			}
		}
	}
	clear();
}

inline void preCalculate()
{
	bt[0][0] = 1; q.push({0, 0});
	while (q.size())
	{
		int y = q.front().fst, x = q.front().snd; q.pop();
		for (int i = 2; i < 4; i++)
		{
			if (inGrid(y + dir[i][0], x + dir[i][1]) && !bt[y + dir[i][0]][x + dir[i][1]] && !ar[y + dir[i][0]][x + dir[i][1]])
			{
				bt[y + dir[i][0]][x + dir[i][1]] = 1;
				q.push({y + dir[i][0], x + dir[i][1]});
			}
		}
	}
	tr[n - 1][m - 1] = 1; q.push({n - 1, m - 1});
	while (q.size())
	{
		int y = q.front().fst, x = q.front().snd; q.pop();
		cnt[y + x]++;
		for (int i = 0; i < 2; i++)
		{
			if (inGrid(y + dir[i][0], x + dir[i][1]) && bt[y + dir[i][0]][x + dir[i][1]] && !tr[y + dir[i][0]][x + dir[i][1]])
			{
				tr[y + dir[i][0]][x + dir[i][1]] = 1;
				q.push({y + dir[i][0], x + dir[i][1]});
			}
		}
	}
	for (int i = 0; i < n; i++) 
	{
		for (int j = 0; j < m; j++) bt[i][j] = 0;
	}
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++) {cin >> ar[i][j];}
	}
	preCalculate();
	cin >> Q;
	for (int i = 0; i < Q; i++)
	{
		int y, x; cin >> y >> x; y--; x--;
		if (!tr[y][x]) {cout << "1\n";}
		else if (cnt[y + x] > 1) {cout << "1\n"; BFS(y, x);}
		else {cout << "0\n";}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 768 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 4 ms 1024 KB Output is correct
5 Correct 4 ms 1024 KB Output is correct
6 Correct 5 ms 1024 KB Output is correct
7 Correct 4 ms 1024 KB Output is correct
8 Correct 4 ms 1152 KB Output is correct
9 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 768 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 3 ms 896 KB Output is correct
4 Correct 4 ms 1024 KB Output is correct
5 Correct 4 ms 1024 KB Output is correct
6 Correct 5 ms 1024 KB Output is correct
7 Correct 4 ms 1024 KB Output is correct
8 Correct 4 ms 1152 KB Output is correct
9 Correct 4 ms 1024 KB Output is correct
10 Correct 10 ms 1024 KB Output is correct
11 Correct 4 ms 768 KB Output is correct
12 Correct 243 ms 7800 KB Output is correct
13 Correct 97 ms 6648 KB Output is correct
14 Correct 326 ms 8440 KB Output is correct
15 Correct 345 ms 8440 KB Output is correct
16 Correct 373 ms 8312 KB Output is correct
17 Correct 395 ms 8920 KB Output is correct
18 Correct 382 ms 8440 KB Output is correct
19 Correct 367 ms 10344 KB Output is correct
20 Correct 352 ms 12260 KB Output is correct
21 Correct 360 ms 10472 KB Output is correct