Submission #835960

#TimeUsernameProblemLanguageResultExecution timeMemory
835960vgoofficialTracks in the Snow (BOI13_tracks)C++14
100 / 100
571 ms158928 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1000000007;
int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(0);
    int h,w;
    cin >> h >> w;
    int field[h][w];
    for(int i = 0; i < h; i++) {
        string s;
        cin >> s;
        for(int j = 0; j < w; j++) {
            if(s[j]=='F') {
                field[i][j]=1;
            } else if(s[j]=='R') {
                field[i][j]=2;
            } else {
                field[i][j]=0;
            }
        }
    }
    deque<pair<int, int>> q;
    q.push_back({0,0});
    vector<vector<int>> dist(h, vector<int>(w, -1));
    dist[0][0]=1;
    int ans = 1;
    vector<int> dx{-1, 1, 0, 0}, dy{0,0,-1,1};
    while(q.size()) {
        pair<int, int> p = q.front();
        q.pop_front();
        for(int i = 0; i < 4; i++) {
            int newx = p.first+dx[i];
            int newy = p.second+dy[i];
            if(newx>=0&&newy>=0&&newx<h&&newy<w&&field[newx][newy]!=0&&dist[newx][newy]==-1) {
                if(field[newx][newy]==field[p.first][p.second]) {
                    dist[newx][newy]=dist[p.first][p.second];
                    q.push_front({newx, newy});
                } else {
                    dist[newx][newy]=dist[p.first][p.second]+1;
                    q.push_back({newx, newy});
                }
                ans = max(ans, dist[newx][newy]);
            }
        }
    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...