Submission #1278785

#TimeUsernameProblemLanguageResultExecution timeMemory
1278785eliminator_101Tracks in the Snow (BOI13_tracks)C++20
100 / 100
774 ms189700 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MAX = 4001, MOD = 1000000007;
ll t = 1, n, m, nx, ny, mx;
vector<string> v(MAX);
vector dist(MAX,vector<ll>(MAX, 1e9));

vector<pair<int, int>> adj = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("checklist.in", "r", stdin);
    //freopen("checklist.out", "w", stdout);
    //cin >>t;
    while(t--) {
        cin >> n >> m;
        for (int i = 0; i < n; i++) cin >> v[i];
        deque<pair<int, int>>q;
        q.push_front({0, 0});
        dist[0][0]=1;
        while(!q.empty()) {
            auto[x, y] = q.front();
            q.pop_front();
            //cout << x << " " << y << endl;
            for (auto[cx, cy]:adj) {
                nx = x+cx;
                ny = y+cy;
                if (nx<0||ny<0||nx>=n||ny>=m||v[nx][ny]=='.')continue;
                if (v[nx][ny]==v[x][y]&&dist[nx][ny]>dist[x][y]) {
                    dist[nx][ny]=dist[x][y];
                    q.push_front({nx, ny});
                }else if (v[nx][ny]!=v[x][y]&&dist[nx][ny]>dist[x][y]+1){
                    dist[nx][ny]=dist[x][y]+1;
                    q.push_back({nx, ny});
                }
            }
        }
        mx = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                //cout << (dist[i][j]==1e9?0:dist[i][j])<< " ";
                if (dist[i][j]==1e9)continue;
                mx = max(mx, dist[i][j]);
            }
            //cout << endl;
        }
        cout << mx << endl;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...