回 帖 发 新 帖 刷新版面

主题:用C语言编写“农夫过河问题”

农夫过河。一个农夫带着一只狼,一只羊和一些菜过河。河边只有一条一船,由
于船太小,只能装下农夫和他的一样东西。在无人看管的情况下,狼要吃羊,羊
要吃菜,请问农夫如何才能使三样东西平安过河。

回复列表 (共20个回复)

沙发

要编程 求这个? 晕

板凳

#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 楼

思想是;先把羊带过去,再把狼带过去,把羊再带过来,再把菜带过去,最后把养再带过去
至于程序,我这个菜鸟就帮不了你了

4 楼

不会游泳的鱼兄弟!你真有闲心呀!佩服!

5 楼

搂主100块要给我了吧

6 楼

二楼的兄弟你编写的程序好像有一点问题哟!
是不是你的思路有点问题啊!
我怎么都运行不出来啊!

7 楼

请问楼主你运行出来了吗

8 楼

我还没有运行出来呢

9 楼

可为什么在TC2。0下不能运行呢 ?
你能说说吗

10 楼

不错 能运行出来  
但是程序太复杂了,
有没有更简单的方法!

我来回复

您尚未登录,请登录后再回复。点此登录或注册