Submission #675787

#TimeUsernameProblemLanguageResultExecution timeMemory
675787NONTACTracks in the Snow (BOI13_tracks)C++11
100 / 100
1439 ms118952 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> ii; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int n, m; char grid[4002][4002]; int d[4002][4002]; int ans = 0; bool same(char c1, char c2) { if(c1 != c2) return true; return false; } void bfs(int si, int sj) { deque<ii> dq; d[si][sj] = 1; dq.push_front(ii(si, sj)); while(!dq.empty()){ int ui = dq.front().first, uj = dq.front().second; dq.pop_front(); ans = max(ans, d[ui][uj]); for(int i = 0; i < 4; i++){ int vi = ui + dx[i], vj = uj + dy[i]; int w = same(grid[ui][uj], grid[vi][vj]); if(vi < 1 || vi > n || vj < 1 || vj > m) continue; if(d[vi][vj] == 0 && grid[vi][vj] != '.'){ d[vi][vj] = d[ui][uj] + w; if(w == 0){ dq.push_front(ii(vi, vj)); } else{ dq.push_back(ii(vi, vj)); } } } } } int main() { //freopen("test.in","r",stdin); //freopen("test.out","w",stdout); cin>>n>>m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin>>grid[i][j]; } } bfs(1, 1); cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...