• 大小: 4KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-04
  • 语言: C/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<i;c++) { printf("step%d:%s\n",c+1,action[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]=“带狼回来.本岸<---(wolf)“;
    conditions[i+1].wolf=0;
    conditions[i+1].sheep=conditions[i].sheep;
    conditions[i+1].cabbage=conditions[i].cabbage;
    }
void takeSheepOver(int i)/*把羊带过去*/
{
    action[i]=“带羊过去.(sheep)--->对岸“;
    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]=“带羊回来.本岸<---(sheep)“;
    conditions[i+1].wolf=conditions[i].wolf;
    conditions[i+1].sheep=0;
    conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeCabbageOver(int i)/*把菜带过去*/
{
    action[i]=“带菜过去.(cabbage)--->对岸“;
    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]=“带菜回来.本岸<---(cabbage)“;
    conditions[i+1].wolf=conditions[i].wolf;
    conditions[i+1].sheep=conditions[i].sheep;
    conditions[i+1].cabbage=0;
}
void getOverBarely(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 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    {
        printf(“step%d

评论

共有 条评论