Submission #1228344

#TimeUsernameProblemLanguageResultExecution timeMemory
1228344ffeyyaae_Tracks in the Snow (BOI13_tracks)C++20
84.69 / 100
2098 ms98400 KiB
#include <bits/stdc++.h>

using namespace std;

using tii = tuple<int,int,int>;

const int N = 4005;

char a[N][N];
int h, w, dp[N][N];
int diry[] = {0, 1, 0, -1}, dirx[] = {1, 0, -1, 0};

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> h >> w;
    for( int i=0;i<h;i++ )
    {
        for( int j=0;j<w;j++ )
        {
            cin >> a[i][j];
        }
    }
    int ans = 0;
    priority_queue<tii, vector<tii>, greater<tii>> pq;
    pq.push( {1, 0, 0} );
    dp[0][0] = 1;
    while( !pq.empty() )
    {
        auto [_, r, c] = pq.top();
        pq.pop();
        ans = max( ans, dp[r][c] );
        for( int i=0;i<4;i++ )
        {
            int nr = r+diry[i], nc = c+dirx[i];
            if( nr>=h || nc>=w || nr<0 || nc<0 || a[nr][nc] == '.' ) continue;
            if( dp[nr][nc] ) continue;
            if( a[r][c] == a[nr][nc] ) dp[nr][nc] = dp[r][c];
            else dp[nr][nc] = dp[r][c]+1;
            pq.push( {dp[nr][nc], nr, nc} );
        }
    }
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...