제출 #1340997

#제출 시각아이디문제언어결과실행 시간메모리
1340997taropolyTracks in the Snow (BOI13_tracks)C++20
100 / 100
500 ms38640 KiB
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
#define TEST
#define all(x) (x).begin(), (x).end()
const vector<pair<int,int>> DIR = { {1,0 } , {-1,0} , {0,1} ,{0,-1}} ;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int H, W;
    cin >> H >> W;
    vector<vector<char>> field( H+1, vector<char>( W+1 ) );
    for (int i =1 ; i <= H ; i++) {
        string s;
        cin >> s;
        for (int j =1 ; j <= W ; j++) {
            field[i][j] = s[j-1];
        }
    }
    int count{0};
    queue<pair<int,int>> q;
    queue<pair<int,int>> pending_q;
    vector<vector<bool>> vis(H+1, vector<bool>(W+1, false));


    char cur_animal = field[1][1];
    vis[1][1] = true;
    q.push({1,1});

    while (!pending_q.empty() || !q.empty()) {
        count++;
        while (!q.empty()) {
            auto [i_u, j_u] = q.front();
            q.pop();
            for (auto [di ,dj] : DIR) {
                int i_v = i_u + di;
                int j_v = j_u + dj;
                if (i_v < 1 || i_v > H || j_v < 1 || j_v > W) { // out of bounds
                    continue;
                }
                if (vis[i_v][j_v]) { continue;} // visited;
                vis[i_v][j_v] = true;
                if (field[i_v][j_v]== '.') {continue;} //empty square
                if ( cur_animal == field[i_v][j_v] ) {
                    q.push({i_v,j_v});
                }else {
                    pending_q.push({i_v,j_v});
                }
            }

        }
        q = pending_q;
        pending_q= {};
        if (cur_animal == 'R') {
            cur_animal = 'F';
        }else {
            cur_animal = 'R';
        }
    }
    cout << count << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...