제출 #1370871

#제출 시각아이디문제언어결과실행 시간메모리
1370871Ekber_EkberTracks in the Snow (BOI13_tracks)C++20
79.17 / 100
749 ms1034860 KiB
#include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
//     #include <debug.h>
// #else
//     #define debug(...)
// #endif
#define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int int16_t
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
constexpr int MAX = 4e+3 + 1;
int n, m;
char v[MAX+2][MAX+2], col[MAX+2][MAX+2];
vector <pair<int16_t, int16_t>> a, b;
pair<int, int> q[MAX*MAX];
int k = -1;

void dfs(int ux, int uy, char c) {
    col[ux][uy] = 1;
    a.pb({ux, uy});
    for (int z = 0; z < 4; z++) {
        int ix = ux + dx[z], iy = uy + dy[z];
        if (ix < 0 || iy < 0 || ix >= n || iy >= m) continue;
        if (v[ix][iy] != c) continue;
        if (col[ix][iy]) continue;
        dfs(ix, iy, c);
    }
}

void bfs(char c) {
    b.clear();
    for (auto &i : a) {
        k++;
        q[k] = i;
    }
    while (k >= 0) {
        auto [ux, uy] = q[k];
        q[k] = {-1, -1};
        k--;
        for (int z = 0; z < 4; z++) {
            int ix = ux + dx[z], iy = uy + dy[z];
            if (ix < 0 || iy < 0 || ix >= n || iy >= m) continue;
            if (col[ix][iy]) continue;
            if (v[ix][iy] != c) continue;
            b.pb({ix, iy});
            k++;
            q[k] = {ix, iy};
            col[ix][iy] = 1;
        }
    }
    a.swap(b);
}

void _() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) cin >> v[i][j];
    }
    dfs(0, 0, v[0][0]);
    array <char, 2> t = {'F', 'R'};
    int id = 0;
    if (v[0][0] == t[0]) id = 1;
    int32_t res = 0;
    while (!a.empty()) {
        bfs(t[id]);
        res++;
        id = 1 - id;
    }
    cout << res;
}

signed main() {

    GOOD_LUCK

    int tests=1;
    // cin >> tests;
    for (int i=1; i <= tests; i++) {
        _();
        cout << endl;
    }

    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…