Submission #1268547

#TimeUsernameProblemLanguageResultExecution timeMemory
1268547buzzy2Tracks in the Snow (BOI13_tracks)C++20
100 / 100
1199 ms405256 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

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

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<vector<char>> v(n,vector<char>(m));
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) cin >> v[i][j];
    }

    vector<vector<int>> dist(n,vector<int>(m,-1));
    deque<tuple<int,int,int>> q;
    q.push_front({0,0,0});

    while(!q.empty()) {
        const auto [i,j,w] = q.front();
        q.pop_front();
        dist[i][j] = w;

        for(const auto& [a,b] : dirs) {
            int ni = a + i, nj = j + b;
            if(ni < 0 || nj < 0 || ni >= n || nj >= m || dist[ni][nj] != -1 || v[ni][nj] == '.') continue;
            
            if(v[ni][nj] != v[i][j]) {
                q.push_back({ni,nj,w+1});
            }
            else {
                q.push_front({ni,nj,w});
            }
        }
    }

    int ans = -1;
    for(const vector<int>& i : dist) for(int j : i) ans = max(ans,j);
    cout<<ans+1<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...