# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1161679 | Madhav_1608 | Tracks in the Snow (BOI13_tracks) | C++20 | 2097 ms | 178468 KiB |
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef pair<ll,ll> pll;
#define double long double
const double eps = 1e-9;
#define FOR(i,a,b) for(long long i=a;i<b;i++)
#define all(v) v.begin(),v.end()
template <typename I>
void print(vector<I> &v){
FOR(i,0,v.size()){cout << v[i] << " ";}
cout << "\n";
}
ll gcd(ll a,ll b){
if(a==0){return b;}
else if(b==0){return a;}
else if(a<b){return gcd(b%a,a);}
else{return gcd(a%b,b);}
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
void setIO(string name = "") {
freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
freopen((name + ".out").c_str(), "w", stdout);
}
void init_code(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void solve(){
// store very big numbers may mean log2
ll h,w;
cin >> h >> w;
vector<string> meadow(h);
FOR(i,0,h) cin >> meadow[i];
vector<bool> visited(h*w,false);
vll dist(h*w,INT_MAX);
priority_queue<pll,vector<pll>,greater<pll>> pq;
pq.push({1,0});
dist[0] = 1;
while(!pq.empty()){
pll curr = pq.top();
pq.pop();
if(visited[curr.second]) continue;
dist[curr.second] = curr.first;
visited[curr.second] = true;
ll r = curr.second/w;
ll c = curr.second%w;
if(c>0){
if(meadow[r][c-1]!='.'){
if(meadow[r][c-1]==meadow[r][c]){
pq.push({curr.first,curr.second-1});
}
else{
pq.push({curr.first+1,curr.second-1});
}
}
}
if(c<(w-1)){
if(meadow[r][c+1]!='.'){
if(meadow[r][c+1]==meadow[r][c]){
pq.push({curr.first,curr.second+1});
}
else{
pq.push({curr.first+1,curr.second+1});
}
}
}
if(r>0){
if(meadow[r-1][c]!='.'){
if(meadow[r-1][c]==meadow[r][c]){
pq.push({curr.first,curr.second-w});
}
else{
pq.push({curr.first+1,curr.second-w});
}
}
}
if(r<(h-1)){
if(meadow[r+1][c]!='.'){
if(meadow[r+1][c]==meadow[r][c]){
pq.push({curr.first,curr.second+w});
}
else{
pq.push({curr.first+1,curr.second+w});
}
}
}
}
ll ans = 0;
FOR(i,0,h*w){
if(dist[i]<INT_MAX) ans = max(ans,dist[i]);
}
cout << ans << "\n";
}
signed main(){
//setIO
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// init_code();
ll t=1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |