Submission #1235537

#TimeUsernameProblemLanguageResultExecution timeMemory
1235537jiayiiyaijTracks in the Snow (BOI13_tracks)C++20
58.44 / 100
2095 ms18424 KiB
#include <iostream>
#include <vector>
#include<queue>
#include <algorithm>
using namespace std;

int main() {
    
    int H, W;
    cin >> H >> W;
    cin.ignore();

    vector<vector<char>> mapp(H, vector<char>(W));
    for (int i=0; i<H; i++) {
        string line;
        getline(cin, line);
        for (int j=0; j<W; j++) { mapp[i][j] = line[j]; }
    }
    
    vector<char> ani(2, '.');
    if (mapp[0][0] == 'R') ani = {'R', 'F'};
    else ani = {'F', 'R'};

    int dirs[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} };
    int count = 0;
    bool flag0 = false, flag1 = false;
    
    while(true) {
        flag0 = false;
        flag1 = false;
        vector<vector<bool>> vis(H, vector<bool>(W, false));
        vis[0][0] = true;
        queue<pair<int, int>> q;
        q.push({0,0});

        while (!q.empty()) {
            int r = q.front().first;
            int c = q.front().second;
            q.pop();

            for (int i = 0; i < 4; i++) {
                int rr = r + dirs[i][0];
                int cc = c + dirs[i][1];
                if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
                if (vis[rr][cc]) continue;
                if (mapp[rr][cc] == '.') continue;

                if (mapp[r][c] == ani[0] || mapp[r][c] == '#') {
                    vis[rr][cc] = true;
                    if (mapp[rr][cc] == ani[0] || mapp[r][c] == ani[0]) flag0 = true;
                    if (mapp[rr][cc] == ani[1]) flag1 = true;
                    q.push({rr, cc});
                }

                if (mapp[r][c] == ani[1]) {
                    if (mapp[rr][cc] == ani[1] || mapp[rr][cc] == '#') {
                        vis[rr][cc] = true;
                        q.push({rr, cc});
                    }
                }
            }

            mapp[r][c] = '#';
        }
        
        if (not flag0) break;
        count += 1;
        if (not flag1) break;
        count += 1;
    }

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