Submission #72575776


Source Code Expand

#include <bits/extc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define repp(i, m, n) for(int i = (int)(m); i < (int)n; i++)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define fore(x, a) for (auto &x : a)
#define rall(x) (x).rbegin(),(x).rend()
#define all(x) (x).begin(),(x).end()
#define co(a, x, b) (a <= x && x < b)
#define INF ((1LL<<62)-(1LL<<31))

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

int main() {
	int h, w;
	cin >> h >> w;
	vector<string> s(h);
	rep(i, h) cin >> s[i];
	
	vector<vi> g(h*w);
	rep(i, h) {
		rep(j, w-1) {
			int k = i*w+j;
			if (s[i][j] != s[i][j+1]) {
				g[k].push_back(k+1);
				g[k+1].push_back(k);
			}
		}
	}
	rep(j, w) {
		rep(i, h-1) {
			int k = i*w+j;
			if (s[i][j] != s[i+1][j]) {
				g[k].push_back(k+w);
				g[k+w].push_back(k);
			}
		}
	}

	vi dist(h*w, -1);
	queue<int> que;
	dist[0] = 0;
	que.push(0);
	while (!que.empty()) {
		int v = que.front();
		que.pop();
		for (int nv : g[v]) {
			if (dist[nv] != -1) continue;
			dist[nv] = dist[v] + 1;
			que.push(nv);
		}
	}

	// rep(i, h) {
	// 	rep(j, w) {
	// 		cout << dist[i*w+j];
	// 	}
	// 	cout << endl;
	// }
	cout << dist[h*w-1] << endl;
}

Submission Info

Submission Time
Task F - EGFパス
User guild2026_130
Language C++23 (GCC 15.2.0)
Score 100
Code Size 1497 Byte
Status AC
Exec Time 222 ms
Memory 63948 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.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
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3600 KiB
00_sample_01.txt AC 1 ms 3628 KiB
00_sample_02.txt AC 1 ms 3612 KiB
01_random_00.txt AC 181 ms 63464 KiB
01_random_01.txt AC 122 ms 46808 KiB
01_random_02.txt AC 9 ms 7152 KiB
01_random_03.txt AC 42 ms 20848 KiB
01_random_04.txt AC 179 ms 63452 KiB
01_random_05.txt AC 173 ms 63564 KiB
01_random_06.txt AC 176 ms 63432 KiB
01_random_07.txt AC 146 ms 63632 KiB
01_random_08.txt AC 167 ms 62308 KiB
01_random_09.txt AC 144 ms 63508 KiB
01_random_10.txt AC 206 ms 63948 KiB
01_random_11.txt AC 222 ms 63704 KiB
01_random_12.txt AC 168 ms 63520 KiB
01_random_13.txt AC 170 ms 63456 KiB
01_random_14.txt AC 15 ms 9520 KiB
01_random_15.txt AC 69 ms 31280 KiB
01_random_16.txt AC 143 ms 55216 KiB
01_random_17.txt AC 119 ms 47472 KiB
01_random_18.txt AC 2 ms 4236 KiB
01_random_19.txt AC 175 ms 63448 KiB
01_random_20.txt AC 97 ms 40428 KiB
01_random_21.txt AC 21 ms 12340 KiB