제출 #1343439

#제출 시각아이디문제언어결과실행 시간메모리
1343439zhaoxwTracks in the Snow (BOI13_tracks)C++20
53.96 / 100
708 ms119028 KiB
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 4005;
int dis[MAX_N][MAX_N],n,m,u,v,x,y,w,ans;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
char c[MAX_N][MAX_N];
string s;
deque<pair<int,int> > q;
int main()
{
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
        cin >> s;
        for(int j = 1;j <= n;j++)
            c[i][j] = s[j-1],dis[i][j] = 2*MAX_N;
    }
    dis[1][1] = 1;
    q.push_back({1,1});
    while(!q.empty())
    {
        tie(u,v) = q.front();
        q.pop_front();
        for(int i = 0;i < 4;i++)
        {
            x = u+dx[i],y = v+dy[i];
            if(x < 1||x > n||y < 1||y > m||c[x][y] == '.') continue;
            if(c[u][v] == c[x][y]) w = 0;
            else w = 1;
            if(dis[x][y] > dis[u][v]+w)
            {
                dis[x][y] = dis[u][v]+w;
                if(w == 1) q.push_back({x,y});
                else q.push_front({x,y});
            }
        }
    }
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++)
            if(c[i][j] != '.')
                ans = max(ans,dis[i][j]);
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...