Submission #1120432

#TimeUsernameProblemLanguageResultExecution timeMemory
1120432vjudge1Tracks in the Snow (BOI13_tracks)C++17
100 / 100
672 ms222700 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"

using namespace std;
using namespace __gnu_pbds;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T, typename key = less_equal<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 4005;
string s[N];
ll dist[N][N], dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, n, m;
bool can_go(ll x, ll y)
{
    return x > 0 and x <= n and y > 0 and y <= m and s[x][y] != '.';
}
void bfs()
{
    for (ll i = 1; i <= n; i++)
    for (ll j = 1; j <= m; j++)
        dist[i][j] = 1e9;
    dist[1][1] = 1;
    deque<pair<ll, ll>> q;
    q.push_back(make_pair(1, 1));
    while (!q.empty())
    {
        auto [x, y] = q.front();
        q.pop_front();
        for (ll k = 0; k < 4; k++)
            if (can_go(x + dx[k], y + dy[k]) and dist[x + dx[k]][y + dy[k]] > dist[x][y] + (s[x][y] != s[x + dx[k]][y + dy[k]]))
            {
                dist[x + dx[k]][y + dy[k]] = dist[x][y] + (s[x][y] != s[x + dx[k]][y + dy[k]]);
                if (s[x][y] != s[x + dx[k]][y + dy[k]]) q.push_back(make_pair(x + dx[k], y + dy[k]));
                else q.push_front(make_pair(x + dx[k], y + dy[k]));
            }
    }
}
void solve()
{
    cin >> n >> m;
    for (ll i = 1; i <= n; i++) cin >> s[i], s[i] = ' ' + s[i];
    bfs();
    ll ans = 0;
    for (ll i = 1; i <= n; i++)
    for (ll j = 1; j <= m; j++) if (s[i][j] != '.') ans = max(ans, dist[i][j]);
    cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...