제출 #1348512

#제출 시각아이디문제언어결과실행 시간메모리
1348512khbaTracks in the Snow (BOI13_tracks)C++20
91.25 / 100
2098 ms105724 KiB
// author: khba

#include "bits/stdc++.h"
using namespace std;

#ifdef khba
#include "C:\Users\Asus\Desktop\khba\debug.h"
#else
#define print(...) 42
#endif

int32_t main() {
#ifdef khba
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (string& s : a) cin >> s;
    vector<vector<int>> dp(n, vector<int>(m, 1e9));
    dp[0][0] = 1;
    int ans = 0;
    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> q;
    q.emplace(1, 0, 0);
    while (q.size()) {
        auto [d, u, v] = q.top();
        q.pop();
        if (dp[u][v] < d) continue;
        ans = max(ans, dp[u][v]);
        if (u > 0 and dp[u - 1][v] > dp[u][v] + (a[u - 1][v] != a[u][v]) and a[u - 1][v] != '.') {
            dp[u - 1][v] = dp[u][v] + (a[u - 1][v] != a[u][v]);
            q.emplace(dp[u - 1][v], u - 1, v);
        }
        if (u + 1 < n and dp[u + 1][v] > dp[u][v] + (a[u + 1][v] != a[u][v]) and a[u + 1][v] != '.') {
            dp[u + 1][v] = dp[u][v] + (a[u + 1][v] != a[u][v]);
            q.emplace(dp[u + 1][v], u + 1, v);
        }
        if (v > 0 and dp[u][v - 1] > dp[u][v] + (a[u][v - 1] != a[u][v]) and a[u][v - 1] != '.') {
            dp[u][v - 1] = dp[u][v] + (a[u][v - 1] != a[u][v]);
            q.emplace(dp[u][v - 1], u, v - 1);
        }
        if (v + 1 < m and dp[u][v + 1] > dp[u][v] + (a[u][v + 1] != a[u][v]) and a[u][v + 1] != '.') {
            dp[u][v + 1] = dp[u][v] + (a[u][v + 1] != a[u][v]);
            q.emplace(dp[u][v + 1], u, v + 1);
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...