主题:用C语言编写“农夫过河问题”
74521rgb
[专家分:0] 发布于 2004-05-12 17:58:00
农夫过河。一个农夫带着一只狼,一只羊和一些菜过河。河边只有一条一船,由
于船太小,只能装下农夫和他的一样东西。在无人看管的情况下,狼要吃羊,羊
要吃菜,请问农夫如何才能使三样东西平安过河。
回复列表 (共20个回复)
沙发
鹰飞 [专家分:0] 发布于 2004-05-12 19:10:00
要编程 求这个? 晕
板凳
不游泳的鱼 [专家分:620] 发布于 2004-05-12 22:25:00
#include <stdio.h>
//0 means it is on this side of the river, 1 means on the other side.
struct Condition {
int farmer;
int wolf;
int sheep;
int cabbage;
};
struct Condition conditions [100];
char* action[100];
void takeWolfOver(int i)
{
action[i] = "Take the wolf over.";
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] = "Take the wolf back.";
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] = "Take the sheep over.";
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] = "Take the sheep back.";
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] = "Take the cabbage over.";
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] = "Take the cabbage back.";
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] = "Get over 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] = "Get back 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 ("%s\n", "Solution:");
for(c=0; c<i; c++)
{
printf ("%d. %s\n", c+1, action[c]);
}
printf ("%s\n", "Ok! Nice job!");
}
void tryOneStep(int i)
{
int c;
int j;
//check for recursion problem.
if(i>=100)
{
printf("%s\n", "Index reached 100. Something is wrong. ");
return;
}
//check for win.
if(conditions[i].farmer==1&&
conditions[i].wolf==1&&
conditions[i].sheep==1&&
conditions[i].cabbage==1)
{
//Yes! Everything is on the other side of the river.
showSolution(i);
return;
}
//check for lose.
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)
)
{
//No! The wolf would eat the sheep or the sheep would eat the cabbage.
return;
}
//check for the same conditions.
for (c=0; c<i; c++)
{
if(conditions[c].farmer==conditions[i].farmer&&
conditions[c].wolf==conditions[i].wolf&&
conditions[c].sheep==conditions[i].sheep&&
conditions[c].cabbage==conditions[i].cabbage)
{
//The same condition occured.
return;
}
}
j=i+1;
if(conditions[i].farmer==0)
{
conditions[j].farmer=1;
getOverBarely(i);
tryOneStep(j);
if(conditions[i].wolf==0)
{
takeWolfOver(i);
tryOneStep(j);
}
if(conditions[i].sheep==0)
{
takeSheepOver(i);
tryOneStep(j);
}
if(conditions[i].cabbage==0)
{
takeCabbageOver(i);
tryOneStep(j);
}
}
else
{
conditions[j].farmer=0;
getBackBarely(i);
tryOneStep(j);
if(conditions[i].wolf==1)
{
takeWolfBack(i);
tryOneStep(j);
}
if(conditions[i].sheep==1)
{
takeSheepBack(i);
tryOneStep(j);
}
if(conditions[i].cabbage==1)
{
takeCabbageBack(i);
tryOneStep(j);
}
}
}
int main()
{
conditions[0].farmer = 0;
conditions[0].wolf = 0;
conditions[0].sheep = 0;
conditions[0].cabbage= 0;
//use recursion to find out how to.
tryOneStep(0);
}
为什么不用C++来面对对象写呢?
3 楼
垃圾棒棒 [专家分:10] 发布于 2004-05-13 11:16:00
思想是;先把羊带过去,再把狼带过去,把羊再带过来,再把菜带过去,最后把养再带过去
至于程序,我这个菜鸟就帮不了你了
4 楼
tangjuan [专家分:0] 发布于 2004-05-15 17:56:00
不会游泳的鱼兄弟!你真有闲心呀!佩服!
5 楼
不游泳的鱼 [专家分:620] 发布于 2004-05-15 20:32:00
搂主100块要给我了吧
6 楼
74521rgb [专家分:0] 发布于 2004-05-19 22:30:00
二楼的兄弟你编写的程序好像有一点问题哟!
是不是你的思路有点问题啊!
我怎么都运行不出来啊!
7 楼
大雪 [专家分:0] 发布于 2004-05-22 16:43:00
请问楼主你运行出来了吗
8 楼
74521rgb [专家分:0] 发布于 2004-05-22 16:48:00
我还没有运行出来呢
9 楼
74521rgb [专家分:0] 发布于 2004-05-22 21:08:00
可为什么在TC2。0下不能运行呢 ?
你能说说吗
10 楼
znk [专家分:0] 发布于 2004-06-14 20:24:00
不错 能运行出来
但是程序太复杂了,
有没有更简单的方法!
我来回复