Submission #951523

#TimeUsernameProblemLanguageResultExecution timeMemory
951523serpent_121Tracks in the Snow (BOI13_tracks)C++17
61.67 / 100
1261 ms396796 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;

int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};

ll depth[4000][4000];

int main() {
    ll h,w; cin >> h >> w;
    ll map[h][w];
    for (int i = 0; i< h; i++) {
        for (int j = 0; j<w; j++) {
            char k; cin >> k;
            if (k=='.') map[i][j] = 0;
            else if (k=='R') map[i][j] = 1;
            else map[i][j] = 2;
        }
    }
    deque<pair<ll,ll>> q; 
    q.push_back({0,0});
    depth[0][0] = 1;
    ll maxdepth = 1;

    while (!q.empty()) {
        ll x= q.front().first; ll y = q.front().second; q.pop_front();
        for (int i = 0; i<4; i++) {
            if (x+dx[i]<w && x+dx[i]>=0 && y+dy[i]<h && y+dy[i]>=0 && map[x+dx[i]][y+dy[i]]!=0) {
                ll a = x+dx[i]; ll b = y+dy[i];
                if (map[a][b]==map[x][y] && depth[a][b]==0) {
                    q.push_front({a,b});
                    depth[a][b] = depth[x][y];
                }
                else {
                    if (depth[a][b]==0 || depth[a][b] > depth[x][y]+1) {
                        depth[a][b] = depth[x][y]+1; q.push_back({a,b});
                        maxdepth = max(maxdepth,depth[a][b]);
                    }
                }
            }
        }
    }

    cout << maxdepth;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...