Submission #382595

#TimeUsernameProblemLanguageResultExecution timeMemory
382595ritul_kr_singhTracks in the Snow (BOI13_tracks)C++17
12.08 / 100
1386 ms543648 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define sp << " " << #define nl << "\n" vector<int> e; int find(int u){ return e[u] < 0 ? u : e[u] = find(e[u]); } void unite(int u, int v){ u = find(u), v = find(v); if(u==v) return; if(e[u] > e[v]) swap(u, v); e[u] += e[v], e[v] = u; } int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; signed main(){ cin.tie(0)->sync_with_stdio(0); int h, w; cin >> h >> w; int g[h][w]; bool vis[h][w]; e.assign(h*w, -1); queue<array<int, 3>> q; for(int i=0; i<h; ++i){ for(int j=0; j<w; ++j){ char c; cin >> c; if(c=='.') g[i][j] = 0; if(c=='F') g[i][j] = 1; if(c=='R') g[i][j] = 2; if(i and g[i-1][j]==g[i][j]) unite(i*w + j, i*w + j - w); if(j and g[i][j-1]==g[i][j]) unite(i*w + j, i*w + j - 1); vis[i][j] = false; } } for(int i=0; i<h; ++i){ for(int j=0; j<w; ++j){ if(find(i*w + j)==find(0)) q.push({i, j, 1}), vis[i][j] = true; } } int ans = 0; while(!q.empty()){ int x = q.front()[0], y = q.front()[1], d = q.front()[2]; q.pop(); for(int k=0; k<4; ++k){ int i = x + dx[k], j = y + dy[k]; if(i>=0 and j>=0 and i!=h and j!=w and g[i][j]==3-g[x][y] and !vis[i][j]){ q.push({i, j, d+1}); vis[i][j] = true; } } ans = max(ans, d); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...