开发工具:
文件大小: 4kb
下载次数: 0
上传时间: 2014-06-10
详细说明: 数据结构,农夫过河。详细讲述过河 #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=100)/*检查循环是否出现问题*/ { printf("%s\n","渡河步骤达到100步出错"); return; } if(conditions[i].farmer==1&&conditions[i].wolf==1&&conditions[i].sheep==1&&conditions[i].cabbage==1)/*检查是否都过河*/ { showSolution(i);/*是的,都过河了.返回*/ return; } if((conditions[i].farmer!=conditions[i].wolf&&conditions[i].wolf==conditions[i].sheep)||(conditions[i].farmer!=conditions[i].sheep&&conditions[i].sheep==conditions[i].cabbage))/*检查是否丢失,出错*/ { /*不,狼会吃掉羊,或者羊会吃掉菜的*/ return; } /*检查条件是否满足*/ for(c=0;c
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.