# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
636660 | LKR__enjoyer | 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>
#include <iostream>
using namespace std;
#define f first
#define s second
int n,m;
char grid[4000][4000];
int odl[4000][4000];
pair<int,int> ruchy[4]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int i,int j)
{
deque<pair<int,int>> w;
w.push_front({i,j});
while(w.empty()==0)
{
pair<int,int> akt=w.front();
w.pop_front();
for(int z=0;z<4;z++)
{
int u1=akt.f+ruchy[z].f;
int u2=akt.s+ruchy[z].s;
if(u1<0||u1>=n||u2<0||u2>=m||grid[u1][u2]=='.')continue;
int waga=0;
if(grid[akt.f][akt.s]!=grid[u1][u2])waga=1;
if(odl[u1][u2]>odl[akt.f][akt.s]+waga)
{
odl[u1][u2]=odl[akt.f][akt.s]+waga;
if(waga==1)w.push_back({u1,u2});
else w.push_front({u1,u2});
}
}
}
}
int main()
{ cin>>n>>m;
for(int i=0;i<n;i++)
{for(int j=0;j<m;j++)
{ if(i||j)odl[i][j]=1e9;
cin>>grid[i][j];
}
}
bfs(0,0);
int wynik=-1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
wynik=max(wynik,odl[i][j]);
cout<<wynik+1;
return 0;
}
bfs(0,0);
int wynik=-1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
wynik=max(wynik,odl[i][j]);
cout<<wynik+1;
return 0;
}