이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |