Editorial for 第3回卬高杯 B問題 - Making Dango
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
解説
cpp17
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
ll dango1(ll x,ll y){
return x*1000000+y;
}
int main() {
ll h,w;cin >> h >> w;
vector<vector<vector<char>>> s(6,vector<vector<char>>(h,vector<char>(w)));
vector<vector<vector<bool>>> t(6,vector<vector<bool>>(h,vector<bool>(w,1)));
vector<vector<vector<int>>> u(6,vector<vector<int>>(h,vector<int>(w,100000000)));
string a;
a="CLDZRG";
ll p;//スタート地点
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin >> s[0][i][j];
if(s[0][i][j]=='S'){
p=dango1(i,j);
u[0][i][j]=0;
}
if(s[0][i][j]=='#')t[0][i][j]=0;
for(int k=1;k<6;k++){
t[k][i][j]=t[0][i][j];
}
}
}
queue<pair<ll,ll>> q;
q.push({p,0});
ll ans=0;
while(!q.empty()){
ll b=q.front().first,c=q.front().second;
ll d=b/1000000,e=b%1000000;
q.pop();
if(!t[c][d][e])continue;
t[c][d][e]=0;
if(c==5&&s[c][d][e]=='G'){
cout << u[c][d][e];
return 0;
}
if(s[c][d][e]==a[c]){
u[c+1][d][e]=min(u[c][d][e],u[c+1][d][e]);
q.push({dango1(d,e),c+1});
continue;
}
if(d!=0 && t[c][d-1][e]){
q.push({dango1(d-1,e),c});
u[c][d-1][e]=min(u[c][d-1][e],u[c][d][e]+1);
}
if(d!=h-1 && t[c][d+1][e]){
q.push({dango1(d+1,e),c});
u[c][d+1][e]=min(u[c][d+1][e],u[c][d][e]+1);
}
if(e!=0 && t[c][d][e-1]){
q.push({dango1(d,e-1),c});
u[c][d][e-1]=min(u[c][d][e-1],u[c][d][e]+1);
}
if(e!=w-1 && t[c][d][e+1]){
q.push({dango1(d,e+1),c});
u[c][d][e+1]=min(u[c][d][e+1],u[c][d][e]+1);
}
}
cout << -1;
}
Comments