# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896643 | tgp07 | 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 <iostream>
#include <fstream>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <numeric>
#include <string>
#include <bitset>
#include <set>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
ll dr[4] = {0, 1, 0, -1};
ll dc[4] = {1, 0, -1, 0};
int main()
{
cin.tie(0) -> sync_with_stdio(0);
ll h, w;
cin >> h >> w;
string grid[h];
for (ll i = 0; i < h; i++) {
cin >> grid[i];
}
deque<pair<pair<ll, ll>, ll>> q;
q.push_front({{0, 0}, 1});
ll dist[h][w];
memset(dist, -1, sizeof(dist));
while (!q.empty()) {
pair<pair<ll, ll>, ll> cur = q.front();
q.pop_front();
if (dist[cur.first.first][cur.first.second] != -1) {
continue;
}
dist[cur.first.first][cur.first.second] = cur.second;
for (ll i = 0; i < 4; i++) {
ll nr = cur.first.first+dr[i];
ll nc = cur.first.second+dc[i];
if (nr>=0 && nr<h && nc>=0 && nc<w && grid[nr][nc]!='.') {
if (grid[cur.first.first][cur.first.second] != grid[nr][nc]) {
q.push_back({{nr, nc}, cur.second+1});
} else {
q.push_front({{nr, nc}, cur.second});
}
}
}
}
ll ans = 0;
for (ll i=0; i<h; i++) {
for (ll j=0; j<w; j++) {
ans=max(ans,dist[i][j]);
}
}
cout<<ans<<"\n";
}