Submission #1313659

#TimeUsernameProblemLanguageResultExecution timeMemory
1313659shirokitoTracks in the Snow (BOI13_tracks)C++20
100 / 100
565 ms119492 KiB
#include <bits/stdc++.h>
using namespace std;

#define ForUp(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define ForDn(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define all(a) (a).begin(), (a).end()

#define fi first
#define se second

using ll = long long;

const int N = 4000 + 24, INF = 1e9;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

int n, m; char a[N][N];
int f[N][N];

bool inside(int i, int j) {
    return (i >= 1 && i <= n && j >= 1 && j <= m);
}

void solve() {
    cin >> n >> m;

    ForUp (i, 1, n) ForUp (j, 1, m) {
        cin >> a[i][j];
    }

    ForUp (i, 1, n) ForUp (j, 1, m) {
        f[i][j] = INF;
    }

    f[1][1] = 1;

    deque<pair<int, int>> q; 
    q.push_back({1, 1});

    while (q.size()) {
        auto [u, v] = q.front();
        q.pop_front();

        ForUp (dir, 0, 3) {
            int i = u + dx[dir], j = v + dy[dir];

            if (!inside(i, j) || a[i][j] == '.') continue;

            int w = (a[u][v] != a[i][j]);

            if (f[u][v] + w < f[i][j]) {
                f[i][j] = f[u][v] + w;
                
                if (w) {
                    q.push_back({i, j});
                }
                
                else {
                    q.push_front({i, j});
                }
            }
        }
    }

    int res = 0;

    ForUp (i, 1, n) ForUp (j, 1, m) {
        if (a[i][j] != '.') res = max(res, f[i][j]);
    }

    cout << res << '\n';
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);

    int T = 1; // cin >> T;
    while (T--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...