Submission #1355063

#TimeUsernameProblemLanguageResultExecution timeMemory
1355063lex._.Tracks in the Snow (BOI13_tracks)C++20
100 / 100
345 ms119040 KiB
#include <bits/stdc++.h>
#define MAX 4000
#define x first
#define y second
using namespace std;

int h, w;
char m[MAX+1][MAX+1];
int cnt[MAX+1][MAX+1];

int dx[4]={0, -1, 0, 1};
int dy[4]={-1, 0, 1, 0};

bool valid(int x, int y)
{
    return (x>=1 && x<=h && y>=1 && y<=w);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> h >> w;
    for(int i=1; i<=h; i++)
    {
        for(int j=1; j<=w; j++)
            cin >> m[i][j];
    }
    deque<pair<int, int>> dq={{1, 1}};
    cnt[1][1]=1;
    int ans=0;
    while(!dq.empty())
    {
        auto [x, y]=dq.front();
        ans=max(ans, cnt[x][y]);
        dq.pop_front();
        for(int d=0; d<4; d++)
        {
            int x_nou=x+dx[d], y_nou=y+dy[d];
            if(valid(x_nou, y_nou) && cnt[x_nou][y_nou]==0 && m[x_nou][y_nou]!='.')
            {
                if(m[x_nou][y_nou]==m[x][y])
                {
                    cnt[x_nou][y_nou]=cnt[x][y];
                    dq.push_front({x_nou, y_nou});
                }
                else
                {
                    cnt[x_nou][y_nou]=cnt[x][y]+1;
                    dq.push_back({x_nou, y_nou});
                }
            }
        }
    }
    cout << ans;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...