#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
/**zagaro & lauren <3**/
#define mod 1000000007 //1e9 + 7
#define pi acos(-1)
#define wl while
#define str string
#define ENDL "\n"
#define sal ' '
#define tp_set ll
#define prc(n) cout.precision(n);cout<<fixed;
#define ord_set tree<tp_set, null_type, less<tp_set>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef bool bl;
typedef char car;
using namespace std;
using namespace __gnu_pbds;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, m, r=0, a, b, v, k;
car c;
cin>>n>>m;
vector<vector<car> > vec(n+2, vector<car> (m+2, '.'));
vector<vector<ll> > res(n+2, vector<ll> (m+2, -1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>vec[i][j];
if(vec[i][j] != '.')res[i][j]= LONG_LONG_MAX;
}
}
queue<tuple<ll,ll> > pq;
pq.push({1, 1});
res[1][1] = 1;
wl(!pq.empty()){
tie(a, b) = pq.front();
pq.pop();
r = max(r, res[a][b]);
if(vec[a+1][b] == vec[a][b])k=0;
else k=1;
if(res[a][b]+k < res[a+1][b]){
res[a+1][b] = res[a][b] + k;
pq.push({a+1, b});
}
if(vec[a-1][b] == vec[a][b])k=0;
else k=1;
if(res[a][b]+k < res[a-1][b]){
res[a-1][b] = res[a][b] + k;
pq.push({a-1, b});
}
if(vec[a][b+1] == vec[a][b])k=0;
else k=1;
if(res[a][b]+k < res[a][b+1]){
res[a][b+1] = res[a][b] + k;
pq.push({a, b+1});
}
if(vec[a][b-1] == vec[a][b])k=0;
else k=1;
if(res[a][b]+k < res[a][b-1]){
res[a][b-1] = res[a][b] + k;
pq.push({a, b-1});
}
}
cout<<r<<ENDL;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |