Submission #590119

#TimeUsernameProblemLanguageResultExecution timeMemory
590119MinhhoTracks in the Snow (BOI13_tracks)C++17
19.79 / 100
747 ms241592 KiB
#define taskname "1" #include <bits/stdc++.h> #define iii tuple<int,int,int> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, maxn = 4010; int a[maxn][maxn], d[maxn][maxn], w, h; deque<iii> dq; bool val(int i, int j) {return min(i, j) >= 1 && i <= w && j <= h;} int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>w>>h; char c; for (int i=1; i<=w; i++) for (int j=1; j<=h; j++) cin>>c, a[i][j] = c != '.' ? c : -1, d[i][j] = maxn * maxn; d[1][1] = 1; dq.emplace_back(1, 1, 1); while (!dq.empty()) { auto [i, j, dij] = dq.front(); dq.pop_front(); if (dij != d[i][j]) continue; for (int k=0; k<4; k++) { int ci = i + dx[k], cj = j + dy[k]; if (val(ci, cj)) { int wt = a[i][j] != a[ci][cj] && min(a[i][j], a[ci][cj]) != -1; if (dij + wt < d[ci][cj]) { d[ci][cj] = dij + wt; if (wt) dq.emplace_back(ci, cj, dij+wt); else dq.emplace_front(ci, cj, dij+wt); } } } } int ans = -1; for (int i=1; i<=w; i++) for (int j=1; j<=h; j++) ans = max(ans, d[i][j]); cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...