이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 4000;
const int B = numeric_limits<int>::max();
const int X[4] = {0, 0, 1, -1}, Y[4] = {1, -1, 0, 0};
int n, m;
string a[N];
deque<pair<int, int>> q;
int d[N][N];
bool valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
q.push_back({0, 0});
for (int i = 0; i < n; i++) {
fill(d[i], d[i] + m, B);
}
d[0][0] = 0;
int c = 0;
while (not q.empty()) {
int x0, y0;
tie(x0, y0) = q.front();
q.pop_front();
c = max(c, d[x0][y0]);
for (int i = 0; i < 4; i++) {
int x1 = x0 + X[i], y1 = y0 + Y[i];
if (valid(x1, y1) && d[x1][y1] > d[x0][y0] + (a[x0][y0] != a[x1][y1])) {
d[x1][y1] = d[x0][y0] + (a[x0][y0] != a[x1][y1]);
if (a[x0][y0] == a[x1][y1]) {
q.push_front({x1, y1});
} else {
q.push_back({x1, y1});
}
}
}
}
cout << c + 1 << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |