#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
//抽象过河状态
struct State{
int local_monk=3;
int local_monster=3;
int remote_monk=0;
int remote_monster=0;
int i_boat=0;
};
int c=0; //计算次数
int m=999;
int boat=0; //船的位置 0在原岸 1在对岸
int action[10][3]={{0,-1,0},{0,-2,0},{0,0,-1},{0,0,-2},{0,-1,-1},{1,1,0},{1,2,0},{1,0,1},{1,0,2},{1,1,1}}; //定义行动数组
vector<State> vi;
//打印结果
void printResullt(){
cout<<endl;
for(int i=0;i<vi.size();i++){
State s=vi[i];
cout<<s.i_boat<<" "<<s.local_monk<<" "<<s.local_monster<<" "<<s.remote_monk<<" "<<s.remote_monster<<endl;
}
cout<<endl;
}
bool isFinalState(State state){
if(state.local_monk==0 && state.local_monster==0 && state.remote_monk==3 && state.remote_monster==3){
printResullt(); //输出结果集合里边的所有状态
if(c<m){
m=c;
}
return true;
}else{
return false;
}
}
bool isEffective(State state){
if((state.local_monk>=state.local_monster && state.remote_monk>=state.remote_monster) || state.local_monk==0 || state.remote_monk==0){
return true;
}else{
return false;
}
}
bool isEmpty(State st){
int n=0;
for(int i=0;i<vi.size();i++){
State s=vi[i];
if(s.local_monk==st.local_monk && s.local_monster==st.local_monster && s.remote_monk==st.remote_monk && s.remote_monster==st.remote_monster && s.i_boat==st.i_boat){
n++;
}
}
if(n>=2){
return true;
}
return false;
}
void search(){
State state=vi.back();
if(isEmpty(state) && vi.size()>1){
return;
}
if(isFinalState(state)){
return;
}
for(int i=0;i<10;i++){
state=vi.back();
if(action[i][0]==boat){
if((boat==0 && state.local_monk>=abs(action[i][1]) && state.local_monster>=abs(action[i][2])) || (boat==1 && state.remote_monk>=abs(action[i][1]) && state.remote_monster>=abs(action[i][2]))){
state.local_monk+=action[i][1];
state.local_monster+=action[i][2];
state.remote_monk-=action[i][1];
state.remote_monster-=action[i][2];
if(isEffective(state)){
boat=(++boat)%2;
state.i_boat=boat;
vi.push_back(state);
c++;
search();
vi.pop_back();
boat=(++boat)%2;
c--;
}
}
}
}
}
int main(){
State state;
vi.push_back(state);
search();
cout<<"最少过河次数为:"<<m<<endl;
}