Submission #598682

#TimeUsernameProblemLanguageResultExecution timeMemory
598682stevancvTracks in the Snow (BOI13_tracks)C++14
5.52 / 100
1203 ms1048576 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 4e3 + 2; int a[N][N], c[N][N], n, m, iden; bool Can(int x, int y) {return 0 <= x && x < n && 0 <= y && y < m && c[x][y] == -1;} void Dfs(int i, int j) { c[i][j] = iden; if (Can(i - 1, j) && a[i - 1][j] == a[i][j]) Dfs(i - 1, j); if (Can(i, j - 1) && a[i][j - 1] == a[i][j]) Dfs(i, j - 1); if (Can(i + 1, j) && a[i + 1][j] == a[i][j]) Dfs(i + 1, j); if (Can(i, j + 1) && a[i][j + 1] == a[i][j]) Dfs(i, j + 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { c[i][j] = -1; if (s[j] == 'F') a[i][j] = 1; if (s[j] == 'R') a[i][j] = 2; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c[i][j] == -1 && a[i][j] > 0) { Dfs(i, j); iden += 1; } } } vector<set<int>> g(iden); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c[i][j] == -1) continue; if (i < n - 1 && c[i + 1][j] != c[i][j] && c[i + 1][j] != -1) g[c[i][j]].insert(c[i + 1][j]); if (j < m - 1 && c[i][j + 1] != c[i][j] && c[i][j + 1] != -1) g[c[i][j]].insert(c[i][j + 1]); } } assert(c[0][0] == c[n - 1][m - 1]); vector<int> dist(iden, 1e9); queue<int> q; dist[0] = 0; q.push(0); while (!q.empty()) { int s = q.front(); q.pop(); for (int u : g[s]) { if (dist[u] > dist[s] + 1) { dist[u] = dist[s] + 1; q.push(u); } } } int ans = 0; for (int i = 0; i < iden; i++) { smax(ans, dist[i] + 1); } cout << ans << en; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...