#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
class Person
{
public:
int code;
Person(){}
Person(int a)
{
code = a;
}
bool operator == (Person P)
{
return P.code == code;
}
}person(1),p0(2);
class Sheep
{
public:
int code;
Sheep() {}
Sheep(int a)
{
code = a;
}
bool operator == (Sheep P)
{
return P.code == code;
}
}sheep(1),s0(0);
class Wolf
{
public:
int code;
Wolf() {}
Wolf(int a)
{
code = a;
}
bool operator == (Wolf P)
{
return P.code == code;
}
}wolf(1),w0(0);
class Vegetable
{
public:
int code;
Vegetable() {}
Vegetable(int a)
{
code = a;
}
bool operator == (Vegetable P)
{
return P.code == code;
}
}vegetable(1),v0(0);
class Shore
{
public:
Person p;
Sheep s;
Wolf w;
Vegetable v;
Shore()
{
p = person;
s = sheep;
w = wolf;
v = vegetable;
}
bool checkShore()
{
bool judge=1;
judge = judge && ((p == p0) && (s == sheep) && (v == vegetable) && (w == wolf));
judge = judge && ((p == p0) && (s == sheep) && (v == vegetable) && (w == w0));
judge = judge && ((p == p0) && (s == sheep) && (v == v0) && (w == wolf));
judge = judge && ((p == person) && (s == s0) && (v == v0) && (w == w0));
judge = judge && ((p == person) && (s == s0) && (v == v0) && (w == wolf));
judge = judge && ((p == person) && (s == s0) && (v == vegetable) && (w == w0));
return judge;
}
void printShore()
{
cout << "原岸:" << endl;
if (p == person) { cout << "人 "; }
if (s == sheep) { cout << "羊 "; }
if (w == wolf) { cout << "狼 "; }
if (v == vegetable) { cout << "菜 "; }
cout << endl << endl;
}
}shore;
void printSolution()
{
int i = 0;
srand((unsigned int)time(NULL));
while (true)
{
if (i % 2 == 0)
{
shore.p = p0;
while (true)
{
int judge = rand()%3+1;
if (judge == 1)
{
shore.s = s0;
if (shore.checkShore())
{
break;
}
shore.s = sheep;
}
else if (judge == 2)
{
shore.v = v0;
if (shore.checkShore())
{
break;
}
shore.v = vegetable;
}
else
{
shore.w = w0;
if (shore.checkShore())
{
break;
}
shore.w = wolf;
}
}
shore.printShore();
}
else
{
shore.p = person;
while (true)
{
int judge = rand() % 4 + 1;
if (judge == 1)
{
shore.s = sheep;
if (shore.checkShore())
{
break;
}
shore.s = s0;
}
else if (judge == 2)
{
shore.v = vegetable;
if (shore.checkShore())
{
break;
}
shore.v = v0;
}
else if(judge==3)
{
shore.w = wolf;
if (shore.checkShore())
{
break;
}
shore.w = w0;
}
else
{
if (shore.checkShore())
{
break;
}
}
}
shore.printShore();
}
i++;
if ((shore.p == p0) && (shore.s == s0) && (shore.v == v0) && (shore.w == w0))
{
break;
}
}
cout << "运送结束" << endl;
}
int main()
{
printSolution();
system("pause");
return 0;
}
什么也不打印,但也不会崩。