修道士与野人问题Word文档格式.doc

上传人:b****9 文档编号:13087015 上传时间:2022-10-04 格式:DOC 页数:16 大小:304KB
下载 相关 举报
修道士与野人问题Word文档格式.doc_第1页
第1页 / 共16页
修道士与野人问题Word文档格式.doc_第2页
第2页 / 共16页
修道士与野人问题Word文档格式.doc_第3页
第3页 / 共16页
修道士与野人问题Word文档格式.doc_第4页
第4页 / 共16页
修道士与野人问题Word文档格式.doc_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

修道士与野人问题Word文档格式.doc

《修道士与野人问题Word文档格式.doc》由会员分享,可在线阅读,更多相关《修道士与野人问题Word文档格式.doc(16页珍藏版)》请在冰豆网上搜索。

修道士与野人问题Word文档格式.doc

逻辑结构设计:

图型结构

存储结构设计:

链式存储结构

采用这种数据结构的好处:

便于采用广度搜索法,得到首先搜索到的边数最少的一条通路,输出一个最佳方案,采用图的邻接表存储结构搜索效率较高。

(2)算法设计

算法设计的总体设计思路为:

在得到修道士人数和小船的容纳人数后,用boatcase得到所有情况,然后再进行安全性检查,以减去修道士少于野人的情况,接着用孩子兄弟结点表示法,将去对面的路作为孩子结点,路与路是兄弟关系,到达另一边时,同样以这种方法,直到找到(0,0,0)。

主要分为4个模块:

boatcase生成所有情况,BFS得到边数最少的最佳方案,safe安全性检测,print输出安全渡河的全过程。

各个模块要完成的主要功能分别为:

生成模块:

生成所有的可能渡河情况

安全检测模块:

对所有的可能渡河情况进行安全检测,,以减去修道士少于野人的情况

广度搜索模块:

采用广度搜索法,得到首先搜索到的边数最少的一条通路

输出模块:

输出所有安全渡河的全过程

主程序的流程图:

建立邻接表

调用函数Linkinit()来进行初始化

把初始状态插入邻接表中

进行广搜找到成功的方案

调用函数Insertson()来插入结点

调用函数guangdu()

打印输出各种方案

调用函数print()

2.2设计表示

(1)函数调用关系图

guangdu

boatcase

safe

insertson

insertbro

print

(2)函数接口规格说明

voidLinkinit(Link**head)

voidinsertson(Link*head,DataTypex)

voidinsertbro(Link*head,DataTypex)

intboatcase(DataTypex,intn)

voidguangdu(Link*p,intn,intc)

intsafe(DataTypex,intn)

voidprint(Link*q,Link*p)

2.3详细设计

Ø

生成模块

{

inti=0,a,b,t=0;

if(x.cw){

a=0;

b=n-a;

while(a+b>

=1)

{

t++;

while(b>

=0)

{

array[i].xds=a;

array[i].yr=b;

i++;

a++;

b--;

}

a=0;

b=n-a-t;

}

}

else

{

a=1;

b=0;

t=0;

while(a+b<

=n)

{

t++;

while(a>

=0)

{

array[i].xds=a*(-1);

array[i].yr=b*(-1);

i++;

a--;

b++;

}

a=array[0].xds*(-1)+t;

b=0;

}

returni;

}

安全检测模块

intsafe(DataTypex,intn)

{

if

((x.xds>

=x.yr||x.xds==0)&

&

((n-x.xds)>

=(n-x.yr)||x.xds==n)&

x.xds>

=0&

x.xds<

=n&

x.yr>

x.yr<

return1;

else

return0;

广度搜索模块

voidguangdu(Link*p,intn,intc)

Link*q,*t;

DataTypetem;

inti,flag1,flag2,g=0,j,count=0;

q=p->

son;

while(q!

=NULL)/

flag1=0;

j=boatcase(q->

data,c);

for(i=0;

i<

j;

i++){

tem.xds=q->

data.xds-array[i].xds;

tem.yr=q->

data.yr-array[i].yr;

tem.cw=1-q->

data.cw;

t=q;

if(safe(tem,n)){

flag2=1;

//1

while(t!

=p)

{

if(tem.xds==t->

data.xds&

tem.yr==t->

data.yr&

tem.cw==t->

data.cw)

{

flag2=0;

break;

}

t=t->

par;

}

if(flag2==1)

{

if(flag1==0)

{

insertson(q,tem);

flag1=1;

else

insertbro(q,tem);

if(tem.xds==0&

tem.yr==0&

tem.cw==0)

print(q,p);

count++;

}

}

}

}

q=q->

next;

}

if(count==0)

printf("

无法成功渡河!

\n"

);

else

有%d种渡河方式。

count);

输出模块

voidprint(Link*q,Link*p)

DataTypea[100];

inti=1;

a[0].cw=0;

a[0].xds=0;

a[0].yr=0;

while(q!

a[i++]=q->

data;

q=q->

while((--i)>

-1)

printf("

(%d%d%d)"

a[i].xds,a[i].yr,a[i].cw);

if(!

(a[i].xds==0&

a[i].yr==0&

a[i].cw==0))

{

if(a[i].cw==1)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1