Submission #138359

#TimeUsernameProblemLanguageResultExecution timeMemory
138359silxikysTracks in the Snow (BOI13_tracks)C++14
2.19 / 100
2023 ms137600 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxn = 4005;
int H, W;
char grid[maxn][maxn];
bool seen[maxn][maxn];

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

bool valid(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

bool cnt(pair<int,int>& p, char curr) {
    for (int k = 0; k < 4; k++) {
        int nx = p.first + dx[k];
        int ny = p.first + dy[k];
        if (!seen[nx][ny] && valid(nx,ny) && grid[nx][ny] == (curr=='R'?'F':'R')) return true;
    }
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> grid[i][j];
        }
    }
    if (H == 1 && W == 1) {
        cout << 1 << '\n';
        return 0;
    }
    queue<pair<int,int>> q, nxt;
    q.push({0,0});
    seen[0][0] = true;
    char curr = grid[0][0];
    int ans = 0;
    while (true) {
        int added = 0;
        //cout << "level " << ans << '\n';
        //cout << q.size() << '\n';
        while (!q.empty()) {
            auto p = q.front(); q.pop();
            nxt.push(p);
            //cout << p.first << ' ' << p.second << '\n';
            for (int k = 0; k < 4; k++) {
                int nx = p.first + dx[k];
                int ny = p.second + dy[k];
                if (!valid(nx,ny)) continue;
                if (seen[nx][ny]) continue;
                if (grid[nx][ny] != curr) continue;
                seen[nx][ny] = true;
                q.push({nx,ny});
                added++;
            }
        }
        while (!nxt.empty()) {
            auto p = nxt.front(); nxt.pop();
            if (cnt(p,curr)) q.push(p);
        }
        /*
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cout << seen[i][j];
            }
            cout << '\n';
        }
        */
        curr = curr == 'R' ? 'F' : 'R';
        if (added == 0) break;
        ans++;
    }
    cout << ans << '\n';
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...