#include<stdlib.h>
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() /*弹栈*/
{
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()
{
int a=1;
link pointer;
Push(3,3,0);
Duhe(3,3,0);
pointer=PPointer1;
cout<<"野人与修道士渡河问题:"<<endl;
cout<<" 河岸左边有三个野人和三个修道士,在左岸有一条船,最多可以两人乘坐,"<<endl;
cout<<" 所有人都会驶船,当野人人数大于修道士人数时,修道士会被杀掉,请问该"<<endl;
cout<<" 如何渡河,所有人才能安全渡河"<<endl;
cout<<"解:"<<endl;
cout<<" 步骤 | 左岸野人数 | 左岸修道士 | 船位置"<<endl;
while(pointer!=NULL)
{
printf(" %d | %d | %d | %d\n",a,pointer->x,pointer->y,pointer->state);
pointer=pointer->next;
i++;
}
getchar();
}
没有合适的资源?快使用搜索试试~ 我知道了~
华农C++实验代码及实验报告
共136个文件
obj:17个
cpp:17个
pdb:16个
5星 · 超过95%的资源 需积分: 22 22 下载量 83 浏览量
2013-10-28
13:51:19
上传
评论
收藏 6.2MB RAR 举报
温馨提示
华农C++实验代码及实验报告 共8个实验: 1.account类 2.mypoint类 3.stackofintegers类 4.扩展mypoint类 5.loan类 6.circle类 7.修改stackofintegers 8.扩展stack类
资源推荐
资源详情
资源评论
收起资源包目录
华农C++实验代码及实验报告 (136个子文件)
TestAccount.cpp 5KB
TestLoan.cpp 1KB
Account.cpp 785B
TestStack.cpp 745B
TestStack.cpp 681B
TestCircle.cpp 664B
TestStack.cpp 520B
StackOfIntegers.cpp 481B
MyPoint3D.cpp 410B
StackOfIntegers.cpp 387B
MyPoint.cpp 369B
MyPoint.cpp 369B
Circle.cpp 282B
Test3D.cpp 227B
Loan.cpp 195B
TestMyPoint.cpp 189B
EmptyStackException.cpp 85B
Exercise12_6.dat 40B
实验三.doc 45KB
实验二.doc 45KB
实验一.doc 43KB
test7.dsp 4KB
test4.dsp 4KB
test3.dsp 4KB
test2.dsp 4KB
test1.dsp 4KB
test6.dsp 4KB
test5.dsp 4KB
test8.dsp 4KB
test4.dsw 518B
test2.dsw 518B
test3.dsw 518B
test5.dsw 518B
test1.dsw 518B
test8.dsw 518B
test6.dsw 518B
test7.dsw 518B
test5.exe 544KB
test3.exe 532KB
test7.exe 532KB
test4.exe 528KB
test2.exe 528KB
test8.exe 524KB
test6.exe 516KB
test1.exe 516KB
Stack.h 997B
Account.h 397B
StackOfIntegers.h 248B
StackOfIntegers.h 248B
MyPoint3D.h 235B
MyPoint.h 210B
MyPoint.h 210B
Circle.h 191B
Loan.h 182B
EmptyStackException.h 140B
vc60.idb 89KB
vc60.idb 81KB
vc60.idb 81KB
vc60.idb 81KB
vc60.idb 81KB
vc60.idb 81KB
vc60.idb 81KB
vc60.idb 73KB
test5.ilk 792KB
test7.ilk 770KB
test3.ilk 768KB
test4.ilk 767KB
test2.ilk 766KB
test8.ilk 758KB
test1.ilk 754KB
test6.ilk 753KB
test1.ncb 57KB
test2.ncb 49KB
test4.ncb 49KB
test5.ncb 49KB
test8.ncb 49KB
test3.ncb 49KB
test6.ncb 49KB
test7.ncb 49KB
TestLoan.obj 274KB
TestStack.obj 242KB
TestStack.obj 240KB
TestStack.obj 158KB
TestCircle.obj 139KB
TestMyPoint.obj 138KB
Test3D.obj 138KB
Account.obj 7KB
StackOfIntegers.obj 6KB
StackOfIntegers.obj 5KB
StackOfIntegers.obj 4KB
MyPoint.obj 4KB
MyPoint.obj 4KB
MyPoint3D.obj 4KB
Circle.obj 3KB
Loan.obj 3KB
EmptyStackException.obj 2KB
test1.opt 53KB
test4.opt 49KB
test2.opt 49KB
test7.opt 49KB
共 136 条
- 1
- 2
资源评论
- zixuanyuan2014-04-23十分有用,刚好是急需的参考
- shentianyou2014-12-06十分有用,刚好是需要的参考
- qq_154242052016-01-19挺全的,可以做为参考,简单可读。
- kind52013142015-07-19C++实验。。参考下。。
- haohao1372014-12-07挺不错的 就是实验报告不全
qq515048602
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功