Submission #968831

#TimeUsernameProblemLanguageResultExecution timeMemory
968831ThunnusTracks in the Snow (BOI13_tracks)C++17
100 / 100
503 ms235784 KiB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define sz(x) (int)(x).size()
#define pii pair<int,int>
#define fi first
#define se second

const int MAX = 4000;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, m, depth[MAX][MAX], ans = 1;
string snow[MAX];

inline bool inside(int x, int y){ return x >= 0 && x < n && y >= 0 && y < m && snow[x][y] != '.';}

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> snow[i];

    deque<pii> dq;
    dq.emplace_back(0, 0);
    depth[0][0] = 1;
    while(!dq.empty()){
        pii c = dq.front();
        dq.pop_front();
        ans = max(ans, depth[c.fi][c.se]);
        for(int i = 0; i < 4; i++){
            int new_x = c.fi + dx[i];
            int new_y = c.se + dy[i];
            if(inside(new_x, new_y) && depth[new_x][new_y] == 0){
                if(snow[new_x][new_y] == snow[c.fi][c.se]){
                    depth[new_x][new_y] = depth[c.fi][c.se];
                    dq.emplace_front(new_x, new_y);
                }
                else{
                    depth[new_x][new_y] = depth[c.fi][c.se] + 1;
                    dq.emplace_back(new_x, new_y);
                }
            }
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...