답안 #943477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943477 2024-03-11T14:11:12 Z NK_ Zoo (COCI19_zoo) C++17
110 / 110
47 ms 10332 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;

const int INF = 1e9 + 10;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M; cin >> N >> M;

	V<string> A(N); for(auto& x : A) cin >> x;

	V<vi> dst(N, vi(M, INF)), vis(N, vi(M, 0));


	dst[0][0] = 0; deque<array<int, 2>> q; q.push_back({0, 0});

	int ans = 0;
	while(sz(q)) {
		auto [r, c] = q.front(); q.pop_front();


		if (vis[r][c]) continue;
		vis[r][c] = 1;
		ans = max(ans, dst[r][c]);

		for(int dr = -1; dr <= 1; dr++) for(int dc = -1; dc <= 1; dc++) {
			if (abs(dr) + abs(dc) != 1) continue;

			int nr = r + dr, nc = c + dc;

			if (nr < 0 || nc < 0 || nr >= N || nc >= M) continue;
			if (A[nr][nc] == '*') continue;

			if (A[nr][nc] != A[r][c]) {
				if (dst[nr][nc] > dst[r][c] + 1) {
					dst[nr][nc] = dst[r][c] + 1;
					q.push_back({nr, nc});
				}
			} else {
				if (dst[nr][nc] > dst[r][c]) {
					dst[nr][nc] = dst[r][c];
					q.push_front({nr, nc});
				}
			}
		}
	}

	cout << ans + 1 << nl;

	exit(0-0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 412 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 412 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 10 ms 10080 KB Output is correct
17 Correct 10 ms 9820 KB Output is correct
18 Correct 9 ms 10332 KB Output is correct
19 Correct 10 ms 10284 KB Output is correct
20 Correct 8 ms 9824 KB Output is correct
21 Correct 42 ms 9664 KB Output is correct
22 Correct 42 ms 9564 KB Output is correct
23 Correct 42 ms 9764 KB Output is correct
24 Correct 43 ms 10200 KB Output is correct
25 Correct 44 ms 10076 KB Output is correct
26 Correct 44 ms 9820 KB Output is correct
27 Correct 41 ms 9668 KB Output is correct
28 Correct 41 ms 9560 KB Output is correct
29 Correct 47 ms 10104 KB Output is correct
30 Correct 43 ms 9816 KB Output is correct