Submission #1343525

#TimeUsernameProblemLanguageResultExecution timeMemory
1343525yuqziiPortals (BOI14_portals)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

using namespace std;

int r, c;

int posToID(int i, int j) {
	return c*i + j;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> r >> c;
	int start = 0, target = 0;
	vector<vector<char>> a(r, vector<char>(c));
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'S')
				start = posToID(i, j);
			else if (a[i][j] == 'C')
				target = posToID(i, j);
		}
	}
	
	vector<vector<int>> preR(r, vector<int>(c)), preC(c, vector<int>(r));
	for (int i = 0; i < r; i++)
		preR[i][0] = a[i][0] == '#';
	for (int j = 0; j < c; j++)
		preC[j][0] = a[0][j] == '#';

	for (int i = 0; i < r; i++)
		for (int j = 1; j < c; j++)
			preR[i][j] = preR[i][j-1] + (a[i][j] == '#');
	for (int i = 1; i < r; i++)
		for (int j = 0; j < c; j++)
			preC[j][i] = preC[j][i-1] + (a[i][j] == '#');

	vector<vector<int>> adj(r * c);
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			if (a[i][j] == '#') continue;

			int u = posToID(i, j);
			bool hasWall = false;
			if (i > 0 && a[i-1][j] != '#') adj[u].push_back(posToID(i-1, j));
			else hasWall = true;
			if (i < r - 1 && a[i+1][j] != '#') adj[u].push_back(posToID(i+1, j));
			else hasWall = true;
			if (j > 0 && a[i][j-1] != '#') adj[u].push_back(posToID(i, j-1));
			else hasWall = true;
			if (j < c - 1 && a[i][j+1] != '#') adj[u].push_back(posToID(i, j+1));
			else hasWall = true;

			if (!hasWall) continue;

			cout << i << ' ' << j << " has wall\n";

			int pLeft = (preR[i][j] == 0)
				? 0
				: lower_bound(preR[i].begin(), preR[i].end(), preR[i][j]) - preR[i].begin() + 1;
			int pRight = upper_bound(preR[i].begin(), preR[i].end(), preR[i][j]) - preR[i].begin() - 1;
			int pUp = (preC[j][i] == 0)
				? 0
				: lower_bound(preC[j].begin(), preC[j].end(), preC[j][i]) - preC[j].begin() + 1;
			int pDown = upper_bound(preC[j].begin(), preC[j].end(), preC[j][i]) - preC[j].begin() - 1;

			adj[u].push_back(posToID(i, pLeft));
			adj[u].push_back(posToID(i, pRight));
			adj[u].push_back(posToID(pUp, j));
			adj[u].push_back(posToID(pDown, j));
		}
	}

	vector<int> dist(r * c, -1);
	queue<pair<int, int>> q;
	q.emplace(start, 0);
	while (!q.empty()) {
		auto [u, d] = q.front();
		q.pop();
		if (dist[u] != -1) continue;

		if (u == target) {
			cout << d << '\n';
			return 0;
		}

		dist[u] = d;
		for (int v : adj[u])
			if (dist[v] == -1) q.emplace(v, d + 1);
	}

	cout << "oops\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...