上传者: sinat_16386019
|
上传时间: 2020-01-03 11:34:25
|
文件大小: 4KB
|
文件类型: c
数据结构,农夫过河。详细讲述过河
#include
//0代表在河的这边;1代表在河的对岸
struct Condition{
int farmer;
int wolf;
int sheep;
int cabbage;
};
struct Condition conditions[100];//结构体条件数组
char * action [100];
void takeWolfOver(int i)
//把狼来过去
{
action[i]="把狼过去.---->对岸";
conditions[i+1].wolf=1;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeWolfBack(int i)/*把狼带回来*/
{
action[i]="带狼回来.本岸对岸";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=1;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeSheepBack(int i)/*把羊带回来*/
{
action[i]="带羊回来.本岸对岸";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=1;
}
void takeCabbageBack(int i)/*把菜带回来*/
{
action[i]="带菜回来.本岸对岸";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;/*全不动*/
}
void getBackBarely(int i)/*返回时的情况*/
{
action[i]="空手回来.本岸<---(barely)";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void showSolution(int i)/*显示解决方法*/
{
int c;
printf("\n");
printf("%s\n","解决办法:");
for(c=0;c