제출 #985029

#제출 시각아이디문제언어결과실행 시간메모리
985029pannenkoekTracks in the Snow (BOI13_tracks)C++17
56.25 / 100
164 ms35828 KiB
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define pb push_back
#define fi first
#define se second

using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;

const int MAXN = 2e3 + 5;
int h, w, dist[MAXN][MAXN];
char field[MAXN][MAXN];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> h >> w;
    fill(&dist[0][0], &dist[0][0] + MAXN * MAXN, -1);
    fill(&field[0][0], &field[0][0] + MAXN * MAXN, '.');

    rep(i, 0, h)
        rep(j, 0, w)
            cin >> field[i + 1][j + 1];

    deque<pii> d{{{1, 1}}};
    dist[1][1] = 1;
    int res = 0;
    while(!d.empty()){
        auto [i, j] = d.front();
        d.pop_front();
        res = dist[i][j];
        for(auto [di, dj]: vector<pii>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}){
            if(field[i + di][j + dj] == '.') continue;
            if(dist[i + di][j + dj] >= 0) continue;
            bool same = field[i + di][j + dj] == field[i][j]; 
            dist[i + di][j + dj] = dist[i][j] + !same;
            if(same) d.push_front({i + di, j + dj});
            else d.push_back({i + di, j + dj});
        }
    }
    cout << res << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...