이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define fi first
#define se second
const int INF = 1e9;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M;
cin >> N >> M;
vector<vector<char>> v(N, vector<char>(M));
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < M; j++) {
v[i][j] = s[j];
}
}
vector<vector<int>> d(N, vector<int>(M, INF));
d[0][0] = 1;
deque<pii> q;
q.push_front(pii(0, 0));
while (!q.empty()) {
pii cur = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int x = cur.fi + dx[i], y = cur.se + dy[i];
if (x < 0 || x >= N || y < 0 || y >= M || v[x][y] == '.') continue;
bool weight = (v[x][y] != v[cur.fi][cur.se]);
if (d[cur.fi][cur.se] + weight < d[x][y]) {
d[x][y] = d[cur.fi][cur.se] + weight;
if (weight) q.push_back(pii(x, y));
else q.push_front(pii(x, y));
}
}
}
int sol = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (v[i][j] != '.') sol = max(sol, d[i][j]);
}
}
cout << sol << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |