紧急求助:哪个数学建模的高手帮忙编一个:"商人过河"的程序,周五就要交,拜托

紧急求助:哪个数学建模的高手帮忙编一个:"商人过河"的程序,周五就要交,拜托,第1张

#include <stdioh>

#include <malloch>

#include <conioh>

/回溯法解决

基本构架

while(){

do{

\\\\\\\

}while(检查是符合格)

if(x[k-1]<=n)

{

if(满足条件){}

else {下一步}

}

else{回溯,尝试下一位置}

}

}/

int a=3,b=3;

//chacuo()中运人两岸需满足商>仆

//注意0商<N仆的现象

int chacuo(int i,int k,int x[])

{

//运两个商人

if(x[k-1]==1)

{

if(i%2==0) {

if(a>=2)

{

if(((a-2)!=0 && a-2<b)||(5-a)<(3-b)) {return -1;}

a=a-2;

}

else return -1;

}

else {

if(a<=1)

{if((a+2<b)||((1-a)!=0&&(1-a)<(3-b))){return -1;}

a=a+2;

}

else return -1;

}

}

//运一个商人

if(x[k-1]==2)

{

if(i%2==0) {

if(a>=1){

if(((a-1)!=0&&a-1<b)||(2-a)<(3-b)){return -1;}

a=a-1;

}

else return -1;

}

else {

if(a<=2){

if((a+1<b)||((2-a)!=0 &&(2-a)<(3-b))){return -1;}

a=a+1;

}

else return -1;

}

}

//运一个仆人

if(x[k-1]==3)

{

if(i%2==0) {

if(b>=1){

if((a!=0&&a<b-1)||((3-a)!=0&&(3-a)<(4-b))){return -1;}

b=b-1;

}

else return -1;

}

else {

if(b<=2)

{

if((a!=0&&a<b+1)||((3-a)!=0&&(3-a)<(2-b))){return -1;}

b=b+1;

}

else return -1;

}

}

//运两个仆人

if(x[k-1]==4)

{

if(i%2==0) {

if(b>=2){

if((a!=0&&a<b-2)||((3-a)!=0&&(3-a)<(5-b))){return -1;}

b=b-2;

}

else return -1;

}

else {

if(b<=1){

if((a!=0&&a<b+2)||((3-a)!=0&&(3-a)<(1-b))){return -1;}

b=b+2;

}

else return -1;

}

}

//运一商一仆

if(x[k-1]==5)

{

if(i%2==0) {

if(a>=1&&b>=1){

if(((a-1)!=0&&a-1<b-1)||(4-a)<(4-b)){return -1;}

a--;b--;

}

else return -1;

}

else {

if(a<=2&&b<=2){

if((a+1<b+1)||((2-a)!=0&&(2-a)<(2-b))){return -1;}

a++;b++;

}

else return -1;

}

}

return 1;

}

//显示

void print(int x[])

{

int i;

for(i=0;x[i]!=0;i++)

{

if((i%2)==0)printf(" 去-->");

else printf(" 返<--");

if(x[i]==1)printf("2商人");

if(x[i]==2)printf("1商人");

if(x[i]==3)printf("1仆人");

if(x[i]==4)printf("2仆人");

if(x[i]==5)printf("1商人1仆人");

}

printf("\n");

}

主函数部分,框架给你了,你动动脑子,自己写吧

#include <stdioh>

#include <stdlibh>

#include <stringh>

#define MAX_STEP 20

//index: 0 - 狼,1-羊,2-菜,3-农夫,value:0-本岸,1-对岸

int a[MAX_STEP][4];

int b[MAX_STEP];

char name[] =

{

"空手",

"带狼",

"带羊",

"带菜"

};

void search(int iStep)

{

int i;

if (a[iStep][0] + a[iStep][1] + a[iStep][2] + a[iStep][3] == 4)

{

for (i = 0; i < iStep; i++)

{

if (a[i][3] == 0)

{

printf("%s到对岸\n", name[b[i] + 1]);

}

else

{

printf("%s回本岸\n", name[b[i] + 1]);

}

}

printf("\n");

return;

}

for (i = 0; i < iStep; i++)

{

if (memcmp(a[i], a[iStep], sizeof(a[i])) == 0)

{

return;

}

}

if (a[iStep][1] != a[iStep][3] && (a[iStep][2] == a[iStep][1] || a[iStep][0] == a[iStep][1]))

{

return;

}

for (i = -1; i <= 2; i++)

{

b[iStep] = i;

memcpy(a[iStep + 1], a[iStep], sizeof(a[iStep + 1]));

a[iStep + 1][3] = 1 - a[iStep + 1][3];

if (i == -1)

{

search(iStep + 1);

}

else if (a[iStep][i] == a[iStep][3])

{

a[iStep + 1][i] = a[iStep + 1][3];

search(iStep + 1);

}

}

}

int main()

{

search(0);

return 0;

}

结果:

带羊到对岸

空手回本岸

带狼到对岸

带羊回本岸

带菜到对岸

空手回本岸

带羊到对岸

带羊到对岸

空手回本岸

带菜到对岸

带羊回本岸

带狼到对岸

空手回本岸

带羊到对岸

Press any key to continue

加我的qq号 476758573

我帮你详细解释再给你流程图啊

虽然时间过了 但希望有用啊

另外,站长团上有产品团购,便宜有保证

给,已经编译运行通过了:

#include<conioh>

#include<stdioh>

#include <stdlibh>

struct node /建立一个类似栈的数据结构并且可以浏览每一个数据点/

{

int x;

int y;

int state;

struct node next;

};

typedef struct node state;

typedef state link;

link PPointer1=NULL;

link PPointer2=NULL;

int a1,b1;

int a2,b2;

/栈中每个数据都分为0,1状态/

void Push(int a,int b,int n)

{

link newnode;

newnode=(link)malloc(sizeof(state));

newnode-> x=a;

newnode-> y=b;

newnode-> state=n;

newnode-> next=NULL;

if(PPointer1==NULL)

{

PPointer1=newnode;

PPointer2=newnode;

}

else

{

PPointer2-> next=newnode;

PPointer2=newnode;

}

}

void Pop() /d栈/

{

link pointer;

if(PPointer1==PPointer2)

{

free(PPointer1);

PPointer1=NULL;

PPointer2=NULL;

}

pointer=PPointer1;

while(pointer-> next!=PPointer2)

pointer=pointer-> next;

free(PPointer2);

PPointer2=pointer;

PPointer2-> next=NULL;

}

int history(int a,int b,int n) /比较输入的数据和栈中是否有重复的/

{

link pointer;

if(PPointer1==NULL)

return 1;

else

{

pointer=PPointer1;

while(pointer!=NULL)

{

if(pointer-> x==a&&pointer-> y==b&&pointer-> state==n)

return 0;

pointer=pointer-> next;

}

return 1;

}

}

int judge(int a,int b,int c,int d,int n) /判断这个状态是否可行,其中使用了history函数/

{

if(history(a,b,n)==0) return 0;

if(a>=0&&b>=0&&a<=3&&b<=3&&c>=0&&d>=0&&c<=3&&d<=3&&a+c==3&&b+d==3)

{

switch(n)

{

case 1:

{

if(a==3)

{

Push(a,b,n);

return 1;

}

else if(a==0)

{

Push(a,b,n);

return 1;

}

else if(a==b)

{

Push(a,b,n);

return 1;

}

else return 0;

}

case 0:

{

if(a==3)

{

Push(a,b,n);

return 1;

}

else if(a==0)

{

Push(a,b,n);

return 1;

}

else if(a>=b)

{

Push(a,b,n);

return 1;

}

else return 0;

}

}

}

else return 0;

}

int Duhe(int a,int b,int n) /递归法解决商人渡河问题,如果这一个状态符合/

{ /则判断下一个状态,直至问题解决/

if(a==0&&b==0) return 1;

if(n==0) /判断0状态时,商匪状态是否符合要求/

{

if(judge(a-1,b-1,4-a,4-b,1))

{

if(Duhe(a-1,b-1,1)==1)

return 1;

}

if(judge(a,b-2,3-a,5-b,1))

{

if(Duhe(a,b-2,1)==1)

return 1;

}

if(judge(a-2,b,5-a,3-b,1))

{

if(Duhe(a-2,b,1)==1)

return 1;

}

if(judge(a-1,b,4-a,3-b,1))

{

if(Duhe(a-1,b,1)==1)

return 1;

}

if(judge(a,b-1,3-a,4-b,1))

{

if(Duhe(a,b-1,1)==1)

return 1;

}

else

{

Pop();

return 0;

}

}

if(n==1) /判断0状态时,商匪状态是否符合要求/

{

if(judge(a+1,b+1,2-a,2-b,0))

{

if(Duhe(a+1,b+1,0)==1)

return 1;

}

if(judge(a,b+2,3-a,1-b,0))

{

if(Duhe(a,b+2,0)==1)

return 1;

}

if(judge(a+2,b,1-a,3-b,0))

{

if(Duhe(a+2,b,0)==1)

return 1;

}

if(judge(a+1,b,2-a,3-b,0))

{

if(Duhe(a+1,b,0)==1)

return 1;

}

if(judge(a,b+1,3-a,2-b,0))

{

if(Duhe(a,b+1,0)==1)

return 1;

}

else

{

Pop();

return 0;

}

}

return 0;

}

main()

{

link pointer;

Push(3,3,0);

Duhe(3,3,0);

pointer=PPointer1;

while(pointer!=NULL)

{

printf( "%d,%d---%d\n ",pointer-> x,pointer-> y,pointer-> state);

pointer=pointer-> next;

}

getch();

}

以上就是关于紧急求助:哪个数学建模的高手帮忙编一个:"商人过河"的程序,周五就要交,拜托全部的内容,包括:紧急求助:哪个数学建模的高手帮忙编一个:"商人过河"的程序,周五就要交,拜托、求程序代码,农夫、狼、羊和白菜过河问题。、用C语言处理商人过河等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/9843954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存