제출 #446111

#제출 시각아이디문제언어결과실행 시간메모리
446111JovanBTracks in the Snow (BOI13_tracks)C++17
100 / 100
655 ms169724 KiB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
int mat[4005][4005];
int n, m;
int mx = 0;
int dist[4005][4005];
 
void bfs(){
    deque <pair <int, int>> q;
    q.push_front({1, 1});
    for(int i=0; i<=n+1; i++){
        for(int j=0; j<=m+1; j++){
            dist[i][j] = 1000000000;
            if(mat[i][j] == 0) dist[i][j] = -500;
        }
    }
    dist[1][1] = 0;
    while(!q.empty()){
        pair <int, int> p = q.front();
        q.pop_front();
        int i = p.first;
        int j = p.second;
        mx = max(mx, dist[i][j]);
        if(mat[i-1][j] == mat[i][j]){
            if(dist[i][j] < dist[i-1][j]){
                dist[i-1][j] = dist[i][j];
                q.push_front({i-1, j});
            }
        }
        else{
            if(dist[i][j] + 1 < dist[i-1][j]){
                dist[i-1][j] = dist[i][j] + 1;
                q.push_back({i-1, j});
            }
        }
        if(mat[i+1][j] == mat[i][j]){
            if(dist[i][j] < dist[i+1][j]){
                dist[i+1][j] = dist[i][j];
                q.push_front({i+1, j});
            }
        }
        else{
            if(dist[i][j] + 1 < dist[i+1][j]){
                dist[i+1][j] = dist[i][j] + 1;
                q.push_back({i+1, j});
            }
        }
        if(mat[i][j-1] == mat[i][j]){
            if(dist[i][j] < dist[i][j-1]){
                dist[i][j-1] = dist[i][j];
                q.push_front({i, j-1});
            }
        }
        else{
            if(dist[i][j] + 1 < dist[i][j-1]){
                dist[i][j-1] = dist[i][j] + 1;
                q.push_back({i, j-1});
            }
        }
        if(mat[i][j] == mat[i][j+1]){
            if(dist[i][j] < dist[i][j+1]){
                dist[i][j+1] = dist[i][j];
                q.push_front({i, j+1});
            }
        }
        else{
            if(dist[i][j] + 1 < dist[i][j+1]){
                dist[i][j+1] = dist[i][j] + 1;
                q.push_back({i, j+1});
            }
        }
    }
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cout.precision(10);
    cout<<fixed;
 
    cin >> n >> m;
    for(int i=1; i<=n; i++){
        string s;
        cin >> s;
        for(int j=1; j<=m; j++){
            if(s[j-1] == 'R') mat[i][j] = 1;
            else if(s[j-1] == 'F') mat[i][j] = -1;
        }
    }
    bfs();
    cout << mx+1 << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...