Submission #1300251

#TimeUsernameProblemLanguageResultExecution timeMemory
1300251kirakosyanTracks in the Snow (BOI13_tracks)C++20
62.19 / 100
2098 ms105552 KiB
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> #include<queue> #include<stack> #include<unordered_map> #include<unordered_set> using namespace std; using ll = long long; ll mod = 1e9 + 7; ll gcd(ll a, ll b) { if (b == 0)return a; else return gcd(b, a % b); } void solve() { int n, m; cin >> n >> m; vector<string>v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } if (v[0][0] == '.') { cout << 0 << endl; return; } int mx = 0; priority_queue<pair<int, pair<int, int>>>q; vector<vector<int>>dist(n, vector<int>(m, 1e9)); vector<int>dx{ 1,-1,0,0 }; vector<int>dy{ 0,0,1,-1 }; q.push({ -1,{0,0} }); dist[0][0] = 1; while (q.size()) { int i = q.top().second.first, j = q.top().second.second, qash = -q.top().first; q.pop(); if (qash != dist[i][j])continue; mx = max(mx, qash); for (int k = 0; k < 4; k++) { if (i + dx[k] < n && i + dx[k] >= 0 && j + dy[k] < n && j + dy[k] >= 0) { if (v[i + dx[k]][j + dy[k]] != '.') { if (qash + (v[i][j] != v[i + dx[k]][j + dy[k]]) < dist[i + dx[k]][j + dy[k]]) { dist[i + dx[k]][j + dy[k]] = qash + (v[i][j] != v[i + dx[k]][j + dy[k]]); q.push({ -(qash + (v[i][j] != v[i + dx[k]][j + dy[k]])),{i + dx[k],j+dy[k]}}); } } } } /*if (i + 1 < n) { if (v[i + 1][j] != '.') { if (qash + (v[i][j] != v[i + 1][j]) < dist[i + 1][j]) { dist[i + 1][j] = qash + (v[i][j] != v[i + 1][j]); q.push({ -(qash + (v[i][j] != v[i + 1][j])),{i + 1,j} }); } } } if (j + 1 < m) { if (v[i][j + 1] != '.') { if (qash + (v[i][j] != v[i][j + 1]) < dist[i][j + 1]) { dist[i][j + 1] = qash + (v[i][j] != v[i][j + 1]); q.push({ -(qash + (v[i][j] != v[i][j + 1])),{i,j + 1} }); } } } if (i - 1 >= 0) { if (v[i - 1][j] != '.') { if (qash + (v[i][j] != v[i - 1][j]) < dist[i - 1][j]) { dist[i - 1][j] = qash + (v[i][j] != v[i - 1][j]); q.push({ -(qash + (v[i][j] != v[i-1][j])),{i-1,j} }); } } } if (j - 1 >= 0) { if (v[i][j - 1] != '.') { if (qash + (v[i][j] != v[i][j - 1]) < dist[i][j - 1]) { dist[i][j - 1] = qash + (v[i][j] != v[i][j - 1]); q.push({ -(qash + (v[i][j] != v[i][j - 1])),{i,j - 1} }); } } }*/ } cout << mx << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); ll t = 1; //cin >> t; while (t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...