제출 #956574

#제출 시각아이디문제언어결과실행 시간메모리
956574jnidvTracks in the Snow (BOI13_tracks)C++17
100 / 100
718 ms135224 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...