Submission #1319284

#TimeUsernameProblemLanguageResultExecution timeMemory
1319284xnoelTracks in the Snow (BOI13_tracks)C++20
31.25 / 100
2140 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("1.in","r",stdin);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> grid(n, vector<int>(m,-1));
    vector<vector<int>> dist(n, vector<int>(m,0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char c;
            cin >> c;
            if (c=='F') grid[i][j]=1;
            else if (c=='R') grid[i][j]=2;
        }
    }

    int ans=0;
    int dx[] = {1,0,-1,0};
    int dy[] = {0,1,0,-1};
    queue<pair<int,int>> q;
    queue<pair<int,int>> q2;
    q.push({0,0});
    int curr_val=grid[0][0];
    int curr_dist=1;
    while (!q.empty()) {
        auto [x,y] = q.front();
        q.pop();
        dist[x][y]=curr_dist;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx<0 || nx>=n || ny<0 || ny>=m) continue;
            if (grid[nx][ny]==-1) continue;
            if (dist[nx][ny]!=0) continue;
            if (grid[nx][ny]==curr_val) {
                q.push({nx,ny});
                //cout<<nx<<" "<<ny<<"\n";
            }
            else q2.push({nx,ny});
        }
        if (q.empty()) {
            if (q2.empty()) break;
            curr_dist++;
            curr_val=3-curr_val;
            while (!q2.empty()) {
                auto pp = q2.front();
                q2.pop();
                q.push(pp);
            }
        }
    }



    cout<<curr_dist<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...