제출 #1336180

#제출 시각아이디문제언어결과실행 시간메모리
1336180JuanJLKraljica (COCI25_kraljica)C++20
0 / 110
158 ms198192 KiB
#include <bits/stdc++.h>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define forn(i,a,b) for(int i = a; i<b; i++)
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;

ll n,m;
vector<string> s;
vector<pair<ll,ll>> oper = {{0,0},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
vector<pair<ll,ll>> tel[20];

ll dijkstra(pair<ll,ll> start, pair<ll,ll> end){

	vector<vector<vector<ll>>> dis(n,vector<vector<ll>>(m,vector<ll>(20,1000000000000000)));
	priority_queue<pair<pair<ll,ll>,pair<ll,ll>>> cola; cola.push({{0,0},start});
	dis[start.fst][start.snd][0]=0;

	while(!cola.empty()){
		ll i = cola.top().snd.fst;
		ll j = cola.top().snd.snd;
		ll c = cola.top().fst.fst*-1;
		ll t = cola.top().fst.snd;
		cola.pop();

				
		if(dis[i][j][t]!=c) continue;
		//cout<<i<<" "<<j<<" " <<t<<" ==>>> "<<c<<'\n';
		
		forn(k,1,SZ(oper)){
			auto o = oper[k];
			ll nc = c + (k!=t?1:0);
			if(i+o.fst<n && i+o.fst>=0 && j+o.snd<m && j+o.snd>=0){
				if(dis[i+o.fst][j+o.snd][k]>nc && s[i+o.fst][j+o.snd]!='#'){
					dis[i+o.fst][j+o.snd][k]=nc;
					cola.push({{nc*-1,k},{i+o.fst,j+o.snd}});
				}
			}
		}

		if(s[i][j]!='.'){
			ll ri = int(s[i][j]-'1');
			if(tel[ri][0]!=pair<ll,ll>{i,j}){
				if(dis[tel[ri][0].fst][tel[ri][0].snd][0]>c){
					dis[tel[ri][0].fst][tel[ri][0].snd][0]=c;
					cola.push({{c*-1,0},tel[ri][0]});
				}
			}else{
				if(dis[tel[ri][1].fst][tel[ri][1].snd][0]>c){
					dis[tel[ri][1].fst][tel[ri][1].snd][0]=c;
					cola.push({{c*-1,0},tel[ri][1]});
				}
			}
		}
	}

	ll best = 1000000000000000;
	forn(i,0,10){
		best=min(best,dis[end.fst][end.snd][i]);
	}

	return best;
}

int main(){
	cin>>n>>m;
	s.clear();
	s.resize(n);

	forn(i,0,n) cin>>s[i];

	pair<ll,ll> start;
	pair<ll,ll> end;
	forn(i,0,n){
		forn(j,0,m){
			if(s[i][j]=='S') start={i,j},s[i][j]='.';
			if(s[i][j]=='E') end={i,j},s[i][j]='.';
			if(s[i][j]!='.' && s[i][j]!='#') tel[int(s[i][j]-'1')].pb({i,j});
		}
		//cout<<s[i]<<'\n';
	}

	cout<<dijkstra(start,end)<<'\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...