# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
955345 | doducanh | 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 ii pair<int,int>
#define fi first
#define se second
int dx[4]={0,1,-1,0};
int dy[4]={-1,0,0,1};
const int maxn=4005;
string s[maxn];
int d[maxn][maxn];
int n,m;
check(int x,int y)
{
return ((x>-1)&&(x<n)&&(y>-1)&&(y<m)&&(s[x][y]!='.')&&(d[x][y]==0));
}
main()
{
cin>>n>>m;
for(int i=0;i<n;i++)cin>>s[i];
d[0][0]=1;
deque<ii>dq;
int ans=0;
dq.push_back({0,0});
while(dq.size()){
ii c=dq.front();
dq.pop_front();
ans=max(ans,d[c.fi][c.se]);
for(int i=0;i<4;i++){
int x=c.fi+dx[i];
int y=c.se+dy[i];
if(check(x,y)){
if(s[c.fi][c.se]!=s[x][y]){
d[x][y]=d[c.fi][c.se]+1;
dq.push_back({x,y});
}
else{
d[x][y]=d[c.fi][c.se];
dq.push_front({x,y});
}
}
}
}
cout<<ans;
}