Submission #71678351


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define debug(arg) cout << "[" << #arg << "]: " << arg << endl
#else
#define debug(arg) 42
#endif

using llu = uint64_t;
using ll = int64_t;

#define vec vector
#define pb push_back
#define all(n) begin(n), end(n)

int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};

void solv() {
	int n, m; cin >> n >> m;
	vec<string> mat(n); for (auto &s : mat) cin >> s;
	vec<vec<pair<int, int>>> adj(26);
	for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (mat[i][j] >= 'a') adj[mat[i][j] - 'a'].pb({i, j});
	auto good = [&](int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && mat[x][y] != '#';};
	vec<vec<int>> dist(n, vec<int>(m, INT_MAX));
	vec<int> xdist(26, INT_MAX);
	deque<tuple<int, int, int>> pq;
	dist[0][0] = 0;
	pq.push_front({0, 0, 0});
	while (!pq.empty()) {
		auto [d, x, y] = pq.front();
		debug(d << ' ' << x << ' ' << y);
		pq.pop_front();
		if (x == n) {
			int c = y;
			for (auto &[x, y] : adj[c]) if (dist[x][y] > d + 1) {
				dist[x][y] = d + 1;
				pq.push_back({d + 1, x, y});
			}
		} else {
			if (int c = mat[x][y] - 'a'; c >= 0 && xdist[c] > d) {
				xdist[c] = d;
				pq.push_front({d, n, c});
			}
			for (int k = 0; k < 4; ++k) {
				int X = x + dx[k], Y = y + dy[k];
				if (!good(X, Y)) continue;
				if (dist[X][Y] > d + 1) {
					dist[X][Y] = d + 1;
					pq.push_back({d + 1, X, Y});
				}
			}
		}
	}
	cout << (dist[n - 1][m - 1] == INT_MAX ? -1 : dist[n - 1][m - 1]) << '\n';
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) solv();
	return 0;
}

Submission Info

Submission Time
Task D - Teleport Maze
User fisher199
Language C++23 (Clang 21.1.0)
Score 400
Code Size 1665 Byte
Status AC
Exec Time 50 ms
Memory 28108 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 51
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3060 KiB
00_sample_01.txt AC 1 ms 3172 KiB
00_sample_02.txt AC 1 ms 3124 KiB
00_sample_03.txt AC 1 ms 2900 KiB
01_random_00.txt AC 1 ms 3100 KiB
01_random_01.txt AC 21 ms 8216 KiB
01_random_02.txt AC 28 ms 8452 KiB
01_random_03.txt AC 48 ms 27632 KiB
01_random_04.txt AC 39 ms 8880 KiB
01_random_05.txt AC 48 ms 13788 KiB
01_random_06.txt AC 42 ms 9652 KiB
01_random_07.txt AC 38 ms 8668 KiB
01_random_08.txt AC 1 ms 3068 KiB
01_random_09.txt AC 15 ms 5020 KiB
01_random_10.txt AC 36 ms 8376 KiB
01_random_11.txt AC 48 ms 23540 KiB
01_random_12.txt AC 47 ms 9052 KiB
01_random_13.txt AC 46 ms 8948 KiB
01_random_14.txt AC 48 ms 9220 KiB
01_random_15.txt AC 44 ms 8860 KiB
01_random_16.txt AC 1 ms 3124 KiB
01_random_17.txt AC 7 ms 4124 KiB
01_random_18.txt AC 18 ms 8628 KiB
01_random_19.txt AC 49 ms 20180 KiB
01_random_20.txt AC 50 ms 14796 KiB
01_random_21.txt AC 50 ms 15988 KiB
01_random_22.txt AC 19 ms 9212 KiB
01_random_23.txt AC 49 ms 12696 KiB
01_random_24.txt AC 1 ms 2900 KiB
01_random_25.txt AC 4 ms 4084 KiB
01_random_26.txt AC 18 ms 8592 KiB
01_random_27.txt AC 40 ms 16096 KiB
01_random_28.txt AC 41 ms 15100 KiB
01_random_29.txt AC 39 ms 12512 KiB
01_random_30.txt AC 41 ms 14488 KiB
01_random_31.txt AC 40 ms 14588 KiB
01_random_32.txt AC 1 ms 3068 KiB
01_random_33.txt AC 11 ms 5964 KiB
01_random_34.txt AC 18 ms 8572 KiB
01_random_35.txt AC 28 ms 12516 KiB
01_random_36.txt AC 19 ms 8964 KiB
01_random_37.txt AC 22 ms 9184 KiB
01_random_38.txt AC 26 ms 11232 KiB
01_random_39.txt AC 27 ms 11236 KiB
02_handmade_00.txt AC 27 ms 8628 KiB
02_handmade_01.txt AC 39 ms 28108 KiB
02_handmade_02.txt AC 18 ms 8668 KiB
02_handmade_03.txt AC 18 ms 8676 KiB
02_handmade_04.txt AC 22 ms 8616 KiB
02_handmade_05.txt AC 28 ms 8640 KiB
02_handmade_06.txt AC 42 ms 17348 KiB