#include #include #include #include #include #include #include #include #include #include template std::vector addn(std::vector a,const std::array b){ for(int i=0;i make_move(const int &cost,const int &b,const int &c,const int &d){ std::array move; move[0]=cost; move[1]=b; move[2]=c; move[3]=d; return move; } int max_index(const std::vector v){ int n=-1; int max_=~0xff; for(unsigned int i=0;i v){ int n=-1; int min_=0xffff; for(unsigned int i=0;iv[i]){ min_=v[i]; n=i; } return n; } template std::vector slice(const std::vector &v,int start,int length=-1){ if(length==-1) length=v.size(); if(start+length>((int)v.size())) length=v.size(); std::vector res; for(int i=start;i std::vector sorted(std::vector v){ std::vector res=v; std::sort(res.begin(),res.end()); return res; } int min(const std::vector &v){ int a=0xffff; for(int n:v) a=std::min(a,n); return a; } bool judge(const std::vector &data){ if(sorted(slice(data,0,3))[1]!=data[1]\ &&data[0]!=data[1]&&data[1]!=data[2]&&data[0]!=data[2]) return true; return false; } std::string data_tostr(const std::vector &data){ using namespace std; ostringstream osm; osm<<"("< void printmap(std::map m){ for(auto iter=m.begin();iter!=m.end();++iter) std::cout< data_; std::array,3> moves_; std::map memo_; public: Kadomatsu(){} Kadomatsu(int a,int b,int c,const std::array,3> &moves__,int cost=0){ std::vector data__; this->data_.push_back(a); this->data_.push_back(b); this->data_.push_back(c); this->data_.push_back(cost); moves_=moves__; } Kadomatsu(const Kadomatsu &dst){ this->data_=dst.data(); this->moves_=dst.moves(); auto tmp=dst.memo(); this->memo_=dst.memo(); } std::vector data()const{return this->data_;} std::array,3> moves()const{return this->moves_;} std::map memo()const{return this->memo_;} int run(){ return this->min_(this->data_); } int min_(std::vector data__){ if(data__[0]<=0||data__[1]<=0||data__[2]<=0){ this->memo_[data_tostr(data__)]=0xffff; return 0xffff; } if(judge(data__)){ this->memo_[data_tostr(data__)]=data__[3]; return data__[3]; } std::vector res; for(auto move:this->moves_){ if(this->memo_.find(data_tostr(addn<4>(data__,move)))!=this->memo_.end()) res.push_back(this->memo_[data_tostr(addn<4>(data__,move))]); else{ res.push_back(this->min_(addn<4>(data__,move))); } } this->memo_[data_tostr(data__)]=min(res); return min(res); } bool find_move(std::vector &res){ std::vector data__; int min_=0xffff; int n; if(judge(this->data_)){ res=this->data_; return false; } for(auto move:this->moves_){ data__=addn<4>(this->data_,move); n=std::min(min_,this->min_(data__)); if(ndata_; res[3]=-1; return false; } return true; } Kadomatsu next(){ std::vector v; Kadomatsu res; this->find_move(v); res=Kadomatsu(v[0],v[1],v[2],this->moves_,v[3]); return res; } bool operator==(const Kadomatsu &dst){ std::vector tmp=dst.data(); for(int i=0;i<3;++i) if(this->data_[i]!=tmp[i]) return false; return true; } }; int main(){ using namespace std; char buf[1024]; string s; vector vs; array,3> moves; vector data; std::vector res; //bool flag; int a,b,c; while(fgets(buf,1024,stdin)){ s=buf; int n; vs.clear(); data.clear(); Kadomatsu k; if(s.find(" ")==string::npos){ } else{ while(true){ n=s.find(" "); if(n==string::npos) break; vs.push_back(s.substr(0,n)); s=s.substr(n+1); } vs.push_back(s); if(vs.size()>=6){ //for(auto s:vs) // cout<<" "<