제출 #421256

#제출 시각아이디문제언어결과실행 시간메모리
421256pavanscodingTracks in the Snow (BOI13_tracks)C++17
100 / 100
789 ms130744 KiB
#include <bits/stdc++.h>
using namespace std;

int dx[4]={0,0,1,-1}, dy[4]={1,-1,0,0};
int n,m,depth[4000][4000], ans=1;
string grid[4000];

bool check(int x, int y){
    return (x>-1&&x<n&&y>-1&&y<m&&grid[x][y]!='.');
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for(int i=0;i<n;i++)
        cin >> grid[i];
    deque<pair<int,int>> q;
    q.push_back({0,0});
    depth[0][0]=1;

    while(!q.empty()){
        pair<int,int> t=q.front();
        q.pop_front();
        ans=max(ans,depth[t.first][t.second]);

        for(int i=0;i<4;i++){
            int x=t.first+dx[i], y=t.second+dy[i];
            if(check(x,y)&&depth[x][y]==0){
                if(grid[x][y]==grid[t.first][t.second]){    
                    depth[x][y]=depth[t.first][t.second];
                    q.push_front({x,y});
                }
                else{
                    depth[x][y]=depth[t.first][t.second]+1;
                    q.push_back({x,y});
                }
            }
        }
    }
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...