이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, M; cin >> N >> M;
vector<string> grid(N); for(auto &x : grid) cin >> x;
vector<vector<int>> depth(N, vector<int>(M, 0));
depth[0][0] = 1;
deque<pair<int, int>> dq;
dq.push_back({0, 0});
int mx = 1;
while(!dq.empty()) {
pair<int, int> cur = dq.front(); dq.pop_front();
mx = max(mx, depth[cur.first][cur.second]);
for(int i = 0; i < 4; i++) {
pair<int, int> next = {cur.first + dx[i], cur.second + dy[i]};
if(next.first < 0 || next.first >= N || next.second < 0 || next.second >= M) continue;
if(depth[next.first][next.second] == 0 && grid[next.first][next.second] != '.') {
if(grid[cur.first][cur.second] == grid[next.first][next.second]) {
depth[next.first][next.second] = depth[cur.first][cur.second];
dq.push_front({next.first, next.second});
} else {
depth[next.first][next.second] = depth[cur.first][cur.second] + 1;
dq.push_back({next.first, next.second});
}
}
}
}
cout << mx << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |