#include<iostream>
#include<ctime>
#include<set>
using namespace std;
struct state
{
//1表示存在,0表示不在
int exi_man;
int exi_sheep;
int exi_wolf;
int exi_veget;
state(){}
state(int a,int b,int c,int d)
{
exi_man = a;
exi_sheep = b;
exi_wolf = c;
exi_veget = d;
}
bool operator==(state a)
{
return a.exi_man == exi_man && a.exi_sheep == exi_sheep && a.exi_veget == exi_veget && a.exi_wolf == exi_wolf;
}
void print_state()
{
if (exi_man) cout << "人";
if (exi_sheep) cout << "羊";
if (exi_wolf) cout << "狼";
if (exi_veget) cout << "菜";
}
bool check_state()
{
bool temp= (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 1)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 0)
|| (exi_man == 0 && exi_sheep == 1 && exi_wolf == 0 && exi_veget == 1)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 1 && exi_veget == 0)
|| (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 0)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 1);
return !temp;
}
void move_in(state a)
{
exi_man += a.exi_man;
exi_sheep += a.exi_sheep;
exi_wolf += a.exi_wolf;
exi_veget += a.exi_veget;
}
void move_out(state a)
{
exi_man -= a.exi_man;
exi_sheep -= a.exi_sheep;
exi_wolf -= a.exi_wolf;
exi_veget -= a.exi_veget;
}
}_state(1,1,1,1);
//共有4种决策:啥也不带,带羊,带狼,带菜
state choice[4] = { {1,0,0,0} ,{1,1,0,0}, {1,0,1,0},{1,0,0,1} };
int record[100] = { -1 };
void print_choice(state a)
{
if (a == choice[0]) cout << "空气 ";
if (a == choice[1]) cout << "羊 ";
if (a == choice[2]) cout << "狼 ";
if (a == choice[3]) cout << "菜 ";
}
int main()
{
int num_step = 0;
while (true)
{
state temp = _state;
if (num_step % 2 == 0)
{
int i = 0;
flag1:
temp = _state;
temp.move_out(choice[i]);
if (temp.exi_man < 0 || temp.exi_man > 1 ||
temp.exi_sheep < 0 || temp.exi_sheep > 1 ||
temp.exi_wolf < 0 || temp.exi_wolf > 1 ||
temp.exi_veget < 0 || temp.exi_veget > 1||
!temp.check_state()) {
i++;
}
if (temp.check_state()) {
record[num_step] = i;
_state = temp;
goto flag;
}
goto flag1;
}
else
{
int i = 0;
flag2:
temp = _state;
temp.move_in(choice[i]);
if (temp.exi_man < 0 || temp.exi_man > 1 ||
temp.exi_sheep < 0 || temp.exi_sheep > 1 ||
temp.exi_wolf < 0 || temp.exi_wolf > 1 ||
temp.exi_veget < 0 || temp.exi_veget > 1||
!temp.check_state()) {
i++;
}
if (temp.check_state()) {
record[num_step] = i;
_state = temp;
goto flag;
}
goto flag2;
}
flag:
num_step++;
if (_state.exi_man == 0 && _state.exi_sheep == 0 && _state.exi_wolf == 0 && _state.exi_veget == 0)
{
cout << "共计:" << num_step << "步" << endl;
for (int i = 0; i < num_step; i++) {
if (i % 2 == 0){
cout << "船夫带走了";
print_choice(choice[i]);
cout << endl;
}
else {
cout << "船夫带回了";
print_choice(choice[i]);
cout << endl;
}
cout << "搬运成功!" << endl;
system("pause");
}
break;
}
}
}
#include<iostream>
#include<ctime>
#include<set>
using namespace std;
struct state
{
//1表示存在,0表示不在
int exi_man;
int exi_sheep;
int exi_wolf;
int exi_veget;
state(){}
state(int a,int b,int c,int d)
{
exi_man = a;
exi_sheep = b;
exi_wolf = c;
exi_veget = d;
}
bool operator==(state a)
{
return a.exi_man == exi_man && a.exi_sheep == exi_sheep && a.exi_veget == exi_veget && a.exi_wolf == exi_wolf;
}
void print_state()
{
if (exi_man) cout << "人";
if (exi_sheep) cout << "羊";
if (exi_wolf) cout << "狼";
if (exi_veget) cout << "菜";
}
bool check_state()
{
bool temp= (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 1)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 0)
|| (exi_man == 0 && exi_sheep == 1 && exi_wolf == 0 && exi_veget == 1)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 1 && exi_veget == 0)
|| (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 0)
|| (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 1);
return !temp;
}
void move_in(state a)//这里的a只代入choice[i]
{
exi_man += a.exi_man;
exi_sheep += a.exi_sheep;
exi_wolf += a.exi_wolf;
exi_veget += a.exi_veget;
}
void move_out(state a)//这里的a只代入choice[i]
{
exi_man -= a.exi_man;
exi_sheep -= a.exi_sheep;
exi_wolf -= a.exi_wolf;
exi_veget -= a.exi_veget;
}
}_state(1,1,1,1);
//共有4种决策:啥也不带,带羊,带狼,带菜
state choice[4] = { {1,0,0,0} ,{1,1,0,0}, {1,0,1,0},{1,0,0,1} };
//如果正确合规,用以记录决策代号
int record[20] = { -1 };
void print_choice(state a)
{
if (a == choice[0]) cout << "空气 ";
if (a == choice[1]) cout << "羊 ";
if (a == choice[2]) cout << "狼 ";
if (a == choice[3]) cout << "菜 ";
}
int main()
{
int num_step = 0;
while (true)
{
state temp = _state;
if (num_step % 2 == 0)
{
int i = 0;
flag1:
temp = _state;
temp.move_out(choice[i]);
if (temp.exi_man < 0 || temp.exi_man > 1 || temp.exi_sheep < 0 || temp.exi_sheep > 1 || temp.exi_wolf < 0 || temp.exi_wolf > 1 || temp.exi_veget < 0 || temp.exi_veget > 1) {
i++;
goto flag1;
}
if (_state.check_state()) {
record[num_step] = i;
_state = temp;
goto flag;
}
}
else
{
int i = 0;
flag2:
temp = _state;
temp.move_in(choice[i]);
if (temp.exi_man < 0 || temp.exi_man > 1 || temp.exi_sheep < 0 || temp.exi_sheep > 1 || temp.exi_wolf < 0 || temp.exi_wolf > 1 || temp.exi_veget < 0 || temp.exi_veget > 1) {
i++;
goto flag2;
}
if (_state.check_state()) {
record[num_step] = i;
_state = temp;
goto flag;
}
goto flag2;
}
flag:
num_step++;
if (_state.exi_man == 0 && _state.exi_sheep == 0 && _state.exi_wolf == 0 && _state.exi_veget == 0)
{
cout << "共计:" << num_step << "步" << endl;
for (int i = 0; i < num_step; i++) {
if (i % 2 == 0){
cout << "船夫带走了";
print_choice(choice[i]);
cout << endl;
}
else {
cout << "船夫带回了";
print_choice(choice[i]);
cout << endl;
}
cout << "搬运成功!" << endl;
system("pause");
}
break;
}
}
}