Submission #367965

#TimeUsernameProblemLanguageResultExecution timeMemory
367965R3KTTracks in the Snow (BOI13_tracks)C++17
38.75 / 100
2095 ms16632 KiB
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <string.h>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
typedef long long ll;

const int MaxN = 4e3;
int h, w, prevsize = 0;
char arr[MaxN][MaxN];

bool dfs(char type) {
    queue<pair<int, int>> q;
    q.push({0,0});
    int count=0;
    while (!q.empty()) {
        pair<int, int> cur = q.front();
        q.pop();
        if (cur.first < 0 || cur.second < 0 || cur.first >= h || cur.second >= w) continue;
        if (arr[cur.first][cur.second] != type) continue;
        count++;
        if (type == 'F') arr[cur.first][cur.second] = 'R';
        else arr[cur.first][cur.second] = 'F';
        q.push({cur.first+1, cur.second});
        q.push({cur.first-1, cur.second});
        q.push({cur.first, cur.second+1});
        q.push({cur.first, cur.second-1});
    }
    bool works = count != prevsize;
    prevsize = count;
    return works;
}

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

    cin >> h >> w;
    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            cin >> arr[i][j];
        }
    }    

    bool fox = false;
    if (arr[0][0] == 'F') fox = true;

    int count=0;
    while (true) {
        char type = 'F';
        if (!fox) type = 'R';
        if (!dfs(type)) break;
        count++;
        fox = !fox;
    }    

    cout << count;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...