Submission #493309

#TimeUsernameProblemLanguageResultExecution timeMemory
493309inksamuraiZoo (COCI19_zoo)C++17
110 / 110
96 ms6284 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3cSpNGp ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;

const int inf=1e9;

const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};

int main(){
_3cSpNGp;
	int h,w;
	cin>>h>>w;
	vec(vec(char)) a(h,vec(char)(w));
	rep(i,h)rep(j,w){
		cin>>a[i][j];
	}
	priority_queue<pii,vec(pii),greater<pii>> pq;
	pq.push({1,0});
	vi dp(h*w,inf);
	dp[0]=1;
	rep(i,h)rep(j,w){
		if(a[i][j]=='*') dp[i*w+j]=0;
	}
	while(sz(pq)){
		pii top=pq.top();
		int v=top.se,cost=top.fi;
		pq.pop();
		if(dp[v]!=cost) continue;
		int i=v/w,j=v%w;
		rep(dir,4){
			int nei=i+di[dir],nej=j+dj[dir];
			int u=nei*w+nej;
			if(min(nei,nej)>=0 and nei<h and nej<w){
				int now=cost;
				if(a[nei][nej]!=a[i][j]) now++;
				if(dp[u]>now){
					dp[u]=now;
					pq.push({now,u});
				}
			}
		}
	}
	cout<<*max_element(all(dp))<<"\n";
//	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...