⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 三名商人各带一个随从乘船渡河
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>

typedef struct OnBoat
{                    //储存船上的人数
    int businessman;
    int servant;
    struct onBoat *next;
}
*onBoat;

typedef struct OnBank
{                   //储存岸上人数的链表
    int businessman1;
    int servant1;
    int businessman2;
    int servant2;
    int n;
    struct oneSide *back;
}
*onSide;

int sign(int n)                       //返回1或者-1
{
    if (n%2==0)
    {
        return -1;
    }
    return 1;
}


int judge(struct OnBank *p)           //判断安全点
{
    struct OnBank *p1=p;
    if (p1->businessman1>3) return -1;
    if (p1->servant1>3) return -1;
    if (p1->businessman2>3) return -1;
    if (p1->servant2>3) return -1;

    if (p1->businessman1<0) return -1;
    if (p1->servant1<0) return -1;
    if (p1->businessman2<0) return -1;
    if (p1->servant2<0) return -1;

    if (p1->businessman1!=0)
    {
        if (p1->servant1>p1->businessman1)
        {
            return -1;
        }
    }

    if (p1->businessman2!=0)
    {
        if (p1->servant2>p1->businessman2)
        {
            return -1;
        }
    }

    while (p1!=NULL)
    {
        p1=p1->back;
        if (p1!=NULL)
        {
            if (p1->n%2==p->n%2)
            {
                if (p1->businessman1==p->businessman1)
                {
                    if (p1->businessman2==p->businessman2)
                    {
                        if (p1->servant1==p->servant1)
                        {
                            if (p1->servant2==p->servant2)
                            {
                                return -1;
                            }
                        }
                    }
                }
            }
        }
    }

    if (p->businessman1==0&&p->servant1==0)
    {
        if (p->n%2==0)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }

    return 0;

}

void copyit(struct OnBank *p3,struct OnBank *p)
{
    p3->businessman1=p->businessman1;
    p3->businessman2=p->businessman2;
    p3->servant1=p->servant1;
    p3->servant2=p->servant2;
    p3->n=p->n+1;
    p3->back=p;

}/*复制内容函数,将p中的内容写入p3所指向的链表单元中*/

void print(struct OnBank *p3)
{
    struct OnBank *p;
    p=p3;
    while (p!=NULL)
    {
        if (p->back==NULL)
        {
            printf("%d,%d::%d,%d\t\n",p->businessman1,p->servant1,p->businessman2,p->servant2);
        }
        else
        {
            printf("%d,%d::%d,%d\t",p->businessman1,p->servant1,p->businessman2,p->servant2);
        }
        p=p->back;
    }

}

void freeit(struct OnBank *p)
{
    struct OnBank *p1=p;
    free(p);
    if (p1!=NULL)
    {
        p1->back=NULL;
    }

}/*释放该单元格,并将其上的单元格的next指针还原*/



void trans(struct OnBank *p,struct OnBoat *head)
{
    struct OnBank *p3;/*p3为申请的结构体指针*/
    struct OnBoat *fla;
    int i;
    int j;
    int f;

    fla=head;

    p3=(struct OnBank *)malloc(sizeof(struct OnBank));
    f=sign(p->n);
    for (i=0;i<5;i++)
    {
        fla=fla->next;
        copyit(p3,p);
        p3->businessman1-=fla->businessman*f;
        p3->servant1-=fla->servant*f;
        p3->businessman2+=fla->businessman*f;
        p3->servant2+=fla->servant*f;/*运算过程,即过河过程*/
        j=judge(p3);/*判断,j记录判断结果*/
        if (j==-1)
        {
            if (i<5)
            {
                continue;
            }
            else
            {
                freeit(p3);
                break;
            }
        }

        if (j==1)
        {
            if (i<5)
            {
                print(p3);
                continue;
            }
            else
            {
                print(p3);
                freeit(p3);
                break;
            }
        }

        if (j==0)
        {
            trans(p3,head);
        }
    }
}/*转移函数,即将人转移过河*/







int main()
{
    int i=0,j=0;
    struct OnBoat *p,*q;
    struct OnBank *p3;

    onBoat head=(struct OnBoat *)malloc(sizeof(struct OnBoat));
    head->businessman=0;
    head->servant=0;
    head->next=NULL;
    p=head;

    for (i=0;i<3;i++)                           //初始化船上人数的链表
    {
        for (j=0;j<3;j++)
        {
            if ((i+j)<3)
            {
                if (i==0&&j==0)
                {
                    continue;
                }
                q=(struct OnBoat *)malloc(sizeof(struct OnBoat));
                q->businessman=i;
                q->servant=j;
                p->next=q;
                q->next=NULL;
                p=q;
            }
        }
    }

    p3=(struct OnBank *)malloc(sizeof(struct OnBank));
    p3->back=NULL;
    p3->servant1=3;
    p3->businessman1=3;
    p3->servant2=0;
    p3->businessman2=0;
    p3->n=1;/*设立初始头指针*/

    trans(p3,head);



    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -