Submission #1318799

#TimeUsernameProblemLanguageResultExecution timeMemory
1318799kutomei3Tracks in the Snow (BOI13_tracks)C++20
100 / 100
653 ms110512 KiB
/**
 * Author: wannaStayWithU
 * Date: 2/2/2569
 */

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

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    char arr[n][m];
    int dp[n][m];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> arr[i][j];
            dp[i][j] = 1e9;
        }
    }

    dp[0][0] = 1;

    deque<pair<int, int>> qu;
    qu.push_front({0, 0});

    int di[4] = {1, -1, 0, 0};
    int dj[4] = {0, 0, -1, 1};

    while (qu.size())
    {
        auto [u, v] = qu.front();
        qu.pop_front();

        for (int p = 0; p < 4; p++) {
            int du = u + di[p];
            int dv = v + dj[p];

            if (du < 0 || du >= n || dv < 0 || dv >= m) continue;
            if (arr[du][dv] == '.' || dp[du][dv] != 1e9) continue;

            if (arr[du][dv] == arr[u][v]) dp[du][dv] = dp[u][v], qu.push_front({du, dv});
            else dp[du][dv] = dp[u][v] + 1, qu.push_back({du, dv});
        }
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (dp[i][j] != 1e9) ans = max(ans, dp[i][j]); 
        }
    }

    cout << ans;

    return 0;
}

/*


*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...