이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9;
vector<int> di = {-1, 0, 0, 1};
vector<int> dj = {0, -1, 1, 0};
void solve() {
int h, w;
cin >> h >> w;
vector<vector<char>> g(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> g[i][j];
}
}
vector<vector<int>> d(h, vector<int>(w, INF));
d[0][0] = 0;
deque<pair<int, int>> dq;
dq.push_front({0, 0});
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int i = p.first, j = p.second;
for (int e = 0; e < 4; e++) {
int ni = i + di[e], nj = j + dj[e];
if (ni < 0 || ni >= h || nj < 0 || nj >= w || g[ni][nj] == '.') continue;
int w = g[i][j] != g[ni][nj] ? 1 : 0;
if (d[i][j] + w < d[ni][nj]) {
d[ni][nj] = d[i][j] + w;
if (w == 1) dq.push_back({ni, nj});
else dq.push_front({ni, nj});
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (d[i][j] != INF && d[i][j] > ans) ans = d[i][j];
}
}
cout << ans + 1 << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |