Submission #1299358

#TimeUsernameProblemLanguageResultExecution timeMemory
1299358Dinh_Van_TungTracks in the Snow (BOI13_tracks)C++20
31.77 / 100
2099 ms79860 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 4005
#define mod 1000000007

int h, w, d[maxn][maxn], dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
char a[maxn][maxn];

void solve() {
    cin >> h >> w;
    for (int i = 1; i <= h; i += 1) for (int j = 1; j <= w; j += 1) {
        cin >> a[i][j];
    }

    memset(d, 0x3f, sizeof d);
    queue<tuple<int, int, int>> que;
    int ans = 0;

    d[1][1] = 1;
    que.push({1, 1, d[1][1]});
    while (que.size()) {
        auto [x, y, dist] = que.front();
        que.pop();
        ans = max(ans, d[x][y]);

        if (dist > d[x][y]) {
            continue;
        }

        for (int i = 0; i < 4; i += 1) {
            int newx = x + dx[i], newy = y + dy[i];
            if (1 <= newx && newx <= h && 1 <= newy && newy <= w && a[newx][newy] != '.') {
                if (d[newx][newy] > dist + (a[x][y] != a[newx][newy])) {
                    d[newx][newy] = dist + (a[x][y] != a[newx][newy]);
                    que.push({newx, newy, d[newx][newy]});
                }
            }
        }
    }
    cout << ans;
    return;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int test_case = 1;
    // cin >> test_case;
    for (int i = 1; i <= test_case; i += 1) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...