# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1120759 | vjudge1 | Tracks in the Snow (BOI13_tracks) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int inf = 2e18,MOD = 1e9+7;
void solve() {
int n,m;
cin >> n >> m;
char grid[n][m];
for (int i=0;i<n;i++) {
string s;
cin >> s;
for (int j = 0;j<m;j++) grid[i][j] = s[j];
}
int dist[n][m];
for (int i=0;i<n;i++) for (int j = 0;j<m;j++) dist[i][j] = inf;
deque<pii> dq;
dist[n-1][m-1] = 1;
dq.push_front({n-1,m-1});
vi dx = {0,1,0,-1},dy = {1,0,-1,0};
while (!dq.empty()) {
pii f = dq.front();
dq.pop_front();
int x = f.ff,y = f.ss;
for (int i = 0;i<4;i++) {
int gx = x+dx[i],gy = y+dy[i];
if (gx < 0 || gx >= n || gy < 0 || gy >= m) continue;
if (grid[gx][gy] == '.') continue;
if (dist[gx][gy] <= dist[x][y]+(grid[gx][gy] != grid[x][y])) continue;
dist[gx][gy] = dist[x][y]+(grid[gx][gy] != grid[x][y]);
if (grid[gx][gy] == grid[x][y]) dq.push_front({gx,gy});
else dq.push_back({gx,gy});
}
}
int ans = 0;
for (int i=0;i<n;i++) for (int j = 0;j<m;j++) if (dist[i][j] != inf) ans = max(ans,dist[i][j]);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while (t --> 0) solve();
}