이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = LLONG_MAX;
vector<string> grid;
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int h, w;
cin >> h >> w;
grid.resize(h);
for (string &x : grid) cin >> x;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<pair<int, int>> q;
q.push_back({0, 0});
dist[0][0] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] != '.' && dist[nx][ny] == INF) {
if (grid[nx][ny] == grid[x][y]) {
dist[nx][ny] = dist[x][y];
q.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
q.push_back({nx, ny});
}
}
}
}
int maxi = -INF;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (dist[i][j] != INF) {
maxi = max(maxi, dist[i][j]);
}
}
}
cout << maxi << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |