제출 #1370848

#제출 시각아이디문제언어결과실행 시간메모리
1370848Ekber_EkberTracks in the Snow (BOI13_tracks)C++20
97.81 / 100
964 ms1114112 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 long long
#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, INF = 2e+9, MOD = 1e+9 + 7, K = 31;
int n, m;
vector <string> v;
vector <vector <int>> col(MAX+2, vector <int>(MAX+2, 0));
vector <pair<int, int>> a, b;

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();
    queue <pair<int, int>> q;
    for (auto [x, y] : a) q.push({x, y});
    while (!q.empty()) {
        auto [ux, uy] = q.front();
        q.pop();
        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});
            q.push({ix, iy});
            col[ix][iy] = 1;
        }
    }
    a.swap(b);
}

void _() {
    cin >> n >> m;
    v.resize(n);
    for (auto &i : v) cin >> i;
    dfs(0, 0, v[0][0]);
    array <char, 2> t = {'F', 'R'};
    int id = 0;
    if (v[0][0] == t[0]) id = 1;
    int 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;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…