Submission #1289411

#TimeUsernameProblemLanguageResultExecution timeMemory
1289411dragstTracks in the Snow (BOI13_tracks)C++20
100 / 100
965 ms333868 KiB
#include <bits/stdc++.h>
#define pll pair<long long, long long>
using namespace std;

char a[4005][4005];
long long dp[4005][4005], check[4005][4005];
deque<pll> q;

int main()
{
    long long n, m, i, j, ans=0;
    cin >> n >> m;
    for (i=1; i<=n; i++)
    {
        for (j=1; j<=m; j++)
        {
            cin >> a[i][j];
        };
    };
    q.push_back(make_pair(1, 1));
    check[1][1]=dp[1][1]=1;
    while (!q.empty())
    {
        i=q.front().first; j=q.front().second;
        q.pop_front();
        ans=max(ans, dp[i][j]);
        if (i>1 && a[i-1][j]!='.' && !check[i-1][j])
        {
            check[i-1][j]=1;
            if (a[i-1][j]==a[i][j]) {dp[i-1][j]=dp[i][j]; q.push_front(make_pair(i-1, j));}
            else {dp[i-1][j]=dp[i][j]+1; q.push_back(make_pair(i-1, j));};
        };
        if (j>1 && a[i][j-1]!='.' && !check[i][j-1])
        {
            check[i][j-1]=1;
            if (a[i][j-1]==a[i][j]) {dp[i][j-1]=dp[i][j]; q.push_front(make_pair(i, j-1));}
            else {dp[i][j-1]=dp[i][j]+1; q.push_back(make_pair(i, j-1));};
        };
        if (i<n && a[i+1][j]!='.' && !check[i+1][j])
        {
            check[i+1][j]=1;
            if (a[i+1][j]==a[i][j]) {dp[i+1][j]=dp[i][j]; q.push_front(make_pair(i+1, j));}
            else {dp[i+1][j]=dp[i][j]+1; q.push_back(make_pair(i+1, j));};
        };
        if (j<m && a[i][j+1]!='.' && !check[i][j+1])
        {
            check[i][j+1]=1;
            if (a[i][j+1]==a[i][j]) {dp[i][j+1]=dp[i][j]; q.push_front(make_pair(i, j+1));}
            else {dp[i][j+1]=dp[i][j]+1; q.push_back(make_pair(i, j+1));};
        };
    };
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...