제출 #895555

#제출 시각아이디문제언어결과실행 시간메모리
895555123456321Tracks in the Snow (BOI13_tracks)C++17
100 / 100
1747 ms119968 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1000000007;
int n,m;
#define pb push_back
#define mp make_pair
vector<vector<pair<int,int>>> adj;


void solve()
{
    cin >> n >>m ;
    vector<string> snow(n);
    for(int i = 0;i<n;i++)cin >> snow[i];
    deque<pair<int,int>> q;
    vector<vector<int>> dist(n,vector<int>(m,0));    
    vector<vector<int>> dir = {{1,0},{-1,0},{0,1},{0,-1}};
    q.push_back({0,0});
    dist[0][0] = 1;
    int ans = 1;
    while(!q.empty())
    {
        auto curr = q.front();
        q.pop_front();
        int x = curr.first;
        int y = curr.second;
        ans = max(ans,dist[x][y]);
        for(auto d : dir)
        {
            int nx = x + d[0];
            int ny = y + d[1];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m
            ||snow[nx][ny]=='.' ||dist[nx][ny]!=0)continue;
				if (snow[x][y] == snow[nx][ny]) {
					dist[nx][ny] = dist[x][y];
					q.push_front({nx, ny});
				} else {
					dist[nx][ny] = dist[x][y] + 1;
					q.push_back({nx, ny});
				}

        }
    }
    cout << ans << "\n";
    return;
}
int main()
{
    int tests = 1;
    //cin >> tests;
    int i = 1;
    while (i <= tests)
    {
        //cout << "Case #"<< i<< ": ";
        solve();
        if(i!=tests)cout << "\n";
        i++;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...