Submission #886002

#TimeUsernameProblemLanguageResultExecution timeMemory
886002noobHeManTracks in the Snow (BOI13_tracks)C++17
100 / 100
408 ms130940 KiB
#include<bits/stdc++.h>
using namespace std;

#define int         int 
#define double      double

const int INF = INT_MAX-10;
const int NINF = -INF;
const int MOD = 998244353;

int dx[4]{-1, 1, 0, 0}, dy[4]{0, 0, -1, 1};

const int maxH = 4005, maxW = 4005;
string grid[maxH];
int dist[maxH][maxW];

int H, W;
bool inside(int x, int y) {
    return x >= 0 && y >= 0 && x < H && y < W && grid[x][y] != '.'; 
}

int ans = 0;
void solve() {
    cin >> H >> W;

    for(int i=0; i<H; i++) {
        cin >> grid[i];
    }

    deque<pair<int, int>> dq;
    dq.push_back({0,0});
    dist[0][0] = 1;

    while(!dq.empty()) {
        pair<int, int> a = dq.front();
        dq.pop_front();
        
        ans = max(ans, dist[a.first][a.second]);

        for(int i=0; i<4; i++) {
            int x = a.first + dx[i], y = a.second + dy[i];
            if(inside(x, y) && dist[x][y] == 0) {
                if(grid[x][y] == grid[a.first][a.second]) {
                    dist[x][y] = dist[a.first][a.second];
                    dq.push_front({x, y});
                } else {
                    dist[x][y] = dist[a.first][a.second] + 1;
                    dq.push_back({x, y});
                }
            }
        }
    }

    cout << ans << "\n";
}

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cout << fixed << setprecision(10);

    int t = 1;
//    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...