Submission #1120596

#TimeUsernameProblemLanguageResultExecution timeMemory
1120596rahidilbayramliTracks in the Snow (BOI13_tracks)C++17
19.79 / 100
1903 ms488424 KiB
#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define sz(v) (ll)(v.size())
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
const ll sz = 4005;
ll lv[sz][sz], vis[sz][sz];
ll dx[] = {1, 0, 0, -1};
ll dy[] = {0, 1, -1, 0};
void solve()
{
    ll h, w, i, j;
    cin >> h >> w;
    char grid[h+5][w+5];
    for(i = 1; i <= h; i++)
    {
        for(j = 1; j <= w; j++)
            cin >> grid[i][j];
    }
    deque<pll>dq;
    dq.pb({1, 1});
    lv[1][1] = 1;
    while(!dq.empty())
    {
        ll x = dq.front().f, y = dq.front().s;
        vis[x][y] = 1;
        dq.pop_front();
        for(i = 0; i < 4; i++)
        {
            ll nx = x + dx[i], ny = y + dy[i];
            if(1 <= nx && nx <= h && 1 <= ny && ny <= w && !vis[nx][ny])
            {
                if(grid[nx][ny] == grid[x][y])
                {
                    lv[nx][ny] = lv[x][y];
                    dq.push_front({nx, ny});
                }
                else
                {
                    lv[nx][ny] = lv[x][y] + 1;
                    dq.pb({nx, ny});
                }
            }
        }
    }
    ll res = 0;
    for(i = 1; i <= h; i++)
    {
        for(j = 1; j <= w; j++)
        {
            if(grid[i][j] != '.')
                res = max(res, lv[i][j]);
        }
    }
    cout << res << "\n";
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll tests = 1;
    //cin >> tests;
    while(tests--)
    {
        solve();
    }
}
/*
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3


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