Submission #71663766


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#define int long long

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

int n, m;
char s[N][N];
int dis[N * N];
bool st[N * N];

int id(int i, int j) {
	return (i - 1) * m + j;
}

int id(char x) {
	return x - 'a' + 1 + n * m;
}

vector<pair<int, int>> g[N * N];

void add(int a, int b, int c) {
	g[a].emplace_back(b, c);
}

signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++ i )
		for (int j = 1; j <= m; ++ j ) {
			cin >> s[i][j];
			if (s[i][j] != '.' && s[i][j] != '#') {
				add(id(i, j), id(s[i][j]), 1);
				add(id(s[i][j]), id(i, j), 0);
			}
		}
	
	for (int i = 1; i <= n; ++ i )
		for (int j = 1; j <= m; ++ j ) if (s[i][j] != '#') {
			for (int k = 0; k < 4; ++ k ) {
				int x = i + dx[k], y = j + dy[k];
				if (x >= 1 && y >= 1 && x <= n && y <= m && s[i][j] != '#') {
					add(id(i, j), id(x, y), 1);
				}
			}
		}
	
	deque<int> q;
	memset(dis, 0x3f, sizeof dis);
	dis[id(1, 1)] = 0;
	q.push_back(id(1, 1));
	while (q.size()) {
		int u = q.front();
		q.pop_front();
		
		if (st[u]) continue;
		st[u] = true;
		
		for (auto [v, w] : g[u]) {
			if (dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				if (w) q.push_back(v);
				else q.push_front(v);
			}
		}
	}
	
	int res = dis[id(n, m)];
	if (res > 1e9) res = -1;
	cout << res;
	
	return 0;
}

Submission Info

Submission Time
Task D - Teleport Maze
User huk2
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1415 Byte
Status AC
Exec Time 304 ms
Memory 202444 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 10 ms 13072 KiB
00_sample_01.txt AC 9 ms 12828 KiB
00_sample_02.txt AC 9 ms 12912 KiB
00_sample_03.txt AC 9 ms 12956 KiB
01_random_00.txt AC 9 ms 13040 KiB
01_random_01.txt AC 100 ms 64552 KiB
01_random_02.txt AC 174 ms 116556 KiB
01_random_03.txt AC 304 ms 199776 KiB
01_random_04.txt AC 186 ms 116992 KiB
01_random_05.txt AC 225 ms 128572 KiB
01_random_06.txt AC 194 ms 117704 KiB
01_random_07.txt AC 188 ms 116812 KiB
01_random_08.txt AC 10 ms 13000 KiB
01_random_09.txt AC 63 ms 43392 KiB
01_random_10.txt AC 163 ms 100980 KiB
01_random_11.txt AC 265 ms 168676 KiB
01_random_12.txt AC 179 ms 101704 KiB
01_random_13.txt AC 177 ms 101648 KiB
01_random_14.txt AC 186 ms 102092 KiB
01_random_15.txt AC 180 ms 101324 KiB
01_random_16.txt AC 10 ms 13228 KiB
01_random_17.txt AC 32 ms 25372 KiB
01_random_18.txt AC 111 ms 84496 KiB
01_random_19.txt AC 226 ms 137452 KiB
01_random_20.txt AC 220 ms 113128 KiB
01_random_21.txt AC 218 ms 119484 KiB
01_random_22.txt AC 127 ms 89104 KiB
01_random_23.txt AC 206 ms 103224 KiB
01_random_24.txt AC 10 ms 13020 KiB
01_random_25.txt AC 21 ms 19868 KiB
01_random_26.txt AC 95 ms 68680 KiB
01_random_27.txt AC 176 ms 106056 KiB
01_random_28.txt AC 183 ms 100072 KiB
01_random_29.txt AC 171 ms 89896 KiB
01_random_30.txt AC 179 ms 96456 KiB
01_random_31.txt AC 175 ms 97228 KiB
01_random_32.txt AC 10 ms 13100 KiB
01_random_33.txt AC 43 ms 32588 KiB
01_random_34.txt AC 73 ms 53148 KiB
01_random_35.txt AC 118 ms 73580 KiB
01_random_36.txt AC 77 ms 56956 KiB
01_random_37.txt AC 84 ms 57456 KiB
01_random_38.txt AC 112 ms 67600 KiB
01_random_39.txt AC 110 ms 66112 KiB
02_handmade_00.txt AC 173 ms 116704 KiB
02_handmade_01.txt AC 231 ms 202444 KiB
02_handmade_02.txt AC 36 ms 14068 KiB
02_handmade_03.txt AC 36 ms 14172 KiB
02_handmade_04.txt AC 95 ms 67984 KiB
02_handmade_05.txt AC 175 ms 115648 KiB
02_handmade_06.txt AC 251 ms 192588 KiB