Submission #440273

#TimeUsernameProblemLanguageResultExecution timeMemory
440273penguinhackerTracks in the Snow (BOI13_tracks)C++14
100 / 100
831 ms152196 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array const int mxN=4000, di[4]={1, -1, 0, 0}, dj[4]={0, 0, 1, -1}; int n, m, d[mxN][mxN], ans; string g[mxN]; bool vis[mxN][mxN]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i=0; i<n; ++i) cin >> g[i]; memset(d, 0x3f, sizeof(d)); d[0][0]=1; deque<ar<int, 2>> q={{0, 0}}; while(q.size()) { int i=q.front()[0], j=q.front()[1]; q.pop_front(); if (vis[i][j]) continue; vis[i][j]=1; ans=max(ans, d[i][j]); for (int k=0; k<4; ++k) { int ni=i+di[k], nj=j+dj[k]; if (0<=ni&&ni<n&&0<=nj&&nj<m&&!vis[ni][nj]&&g[ni][nj]^'.') { if (g[i][j]==g[ni][nj]&&d[i][j]<d[ni][nj]) { d[ni][nj]=d[i][j]; q.push_front({ni, nj}); } else if (g[i][j]^g[ni][nj]&&d[i][j]+1<d[ni][nj]) { d[ni][nj]=d[i][j]+1; q.push_back({ni, nj}); } } } } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...