Submission #648135

#TimeUsernameProblemLanguageResultExecution timeMemory
648135veigaTracks in the Snow (BOI13_tracks)C++17
16.46 / 100
2149 ms1048576 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
#define endl "\n"
const int INF = 1e18+10;
const int MOD = 1e9+7;

int n, m;
string t[4040];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int cr = 0, cf = 0;

void bfs(char c) {
    queue<pair<int, int>> q;
    q.push({0, 0});
    while(!q.empty()) {
        int x = q.front().F, y = q.front().S; q.pop();
        if(c == 'R') {
            t[x][y] = 'F';
            cf++;
            cr--;
        }
        else {
            t[x][y] = 'R';
            cr++;
            cf--;
        }
        for(int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx < 0 or xx >= n or yy < 0 or yy >= m) continue;
            if(t[xx][yy] != c) continue;
            q.push({xx, yy});
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        cin >> t[i];
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(t[i][j] == 'R') cr++;
            if(t[i][j] == 'F') cf++;
        }
    }
    int resp = 0;
    while(cf != 0 and cr != 0) {
        resp++;
        bfs(t[0][0]);
    }
    cout << resp+1 << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...