Submission #748310

#TimeUsernameProblemLanguageResultExecution timeMemory
74831054skyxenonTracks in the Snow (BOI13_tracks)C++17
100 / 100
698 ms175804 KiB
// https://oj.uz/problem/view/BOI13_tracks
 
#include <bits/stdc++.h>
using namespace std;
 
#define tiii tuple<int, int, int>
#define mp make_pair
#define mt make_tuple
#define maxHW 4000
 
bool seen[maxHW][maxHW];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int h, w;
    cin >> h >> w;
 
    vector<string> grid(h);
    for (int i = 0; i < h; i++) {
        cin >> grid[i];
    }
 
    int ans = 0;
 
    deque<tiii> Q({mt(0, 0, 1)});
    while (!Q.empty()) {
        auto [r, c, depth] = Q.front(); Q.pop_front();
        ans = max(ans, depth);
        seen[r][c] = true;
 
        for (auto [nr, nc] : {mp(r + 1, c), mp(r - 1, c), mp(r, c + 1), mp(r, c - 1)}) {
            if (0 <= nr && nr < h && 0 <= nc && nc < w && !seen[nr][nc] && grid[nr][nc] != '.') {
                if (grid[nr][nc] == grid[r][c]) {
                    Q.push_front(mt(nr, nc, depth));
                }
                else {
                    Q.push_back(mt(nr, nc, depth + 1));
                }
            }
        }
    }
 
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...