| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3194 人关注过本帖
标题:[求助]一道实验报告题(迷宫求解)
只看楼主 加入收藏
wizardkid
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-4-14
收藏
 问题点数:0 回复次数:4 
[求助]一道实验报告题(迷宫求解)
题目是迷宫求解,因为是菜鸟,不是很懂得其中的原理,
求高手给我个算法和源代码,谢谢
搜索更多相关主题的帖子: 实验报告 迷宫 源代码 求解 
2007-05-10 21:38
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

typedef struct{
int x,y;
int dir;
}pos,elem;

typedef struct{
elem* b,* t;
int size;
}stack;

void initstack(stack* s)
{
s->b=(elem*)malloc(50*sizeof(elem));
if(s->b){
s->t=s->b;
s->size=50;
return;
}
exit(0);
}
void push(stack* s,elem* e)
{
*s->t=*e;
s->t++;
}
void pop(stack* s,elem* e)
{
*e=*--s->t;
}
void gettop(stack* s,elem* e)
{
*e=*(s->t-1);
}
void clearstack(stack* s)
{
s->t=s->b;
}
int stackempty(stack* s)
{
return !(s->t-s->b);
}
int destroystack(stack* s)
{
free(s->b);
free(s);
return 1;
}
int mg[10][10]={
{-1,-0,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-0,-0,-0,-1,-0,-0,-0,-0,-1},
{-1,-0,-1,-0,-1,-0,-1,-1,-0,-1},
{-1,-0,-1,-1,-0,-0,-0,-0,-0,-1},
{-1,-0,-0,-1,-0,-1,-1,-0,-0,-1},
{-1,-0,-0,-0,-0,-0,-1,-0,-0,-1},
{-1,-0,-1,-0,-0,-0,-0,-1,-0,-1},
{-1,-0,-1,-1,-0,-0,-0,-1,-0,-1},
{-1,-0,-0,-0,-1,-0,-0,-1,-0,-0},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
};

void step(stack* s,pos* cur,stack* result);
void savepath(stack* s,pos* cur,stack* result);
void draw(int y,int x);

void step(stack* s,pos* cur,stack* result)
{
if(stackempty(result)||s->t-s->b<result->t-result->b){
for(cur->dir++;cur->dir<5;cur->dir++){
setfillstyle(SOLID_FILL,15);
switch(cur->dir){
case 1:
if(!mg[cur->y-1][cur->x]){
mg[cur->y][cur->x]=1;
push(s,cur);
cur->y--;
cur->dir=0;
draw(cur->y,cur->x);
return;
}
break;
case 2:
if(!mg[cur->y][cur->x+1]){
mg[cur->y][cur->x]=1;
push(s,cur);
cur->x++;
cur->dir=0;
draw(cur->y,cur->x);
return;
}
break;
case 3:
if(!mg[cur->y+1][cur->x]){
mg[cur->y][cur->x]=1;
push(s,cur);
cur->y++;
cur->dir=0;
draw(cur->y,cur->x);
return;
}
break;
case 4:
if(!mg[cur->y][cur->x-1]){
mg[cur->y][cur->x]=1;
push(s,cur);
cur->x--;
cur->dir=0;
draw(cur->y,cur->x);
return;
}
break;
default:
exit(0);
}
}
}
mg[cur->y][cur->x]=0;
setfillstyle(SOLID_FILL,0);
draw(cur->y,cur->x);
pop(s,cur);
}

void savepath(stack* s,pos* cur,stack* result)
{
pos* top=s->t;
if(stackempty(result)){
push(result,cur);
while(top>s->b)
push(result,--top);
}
else if(result->t-result->b>s->t-s->b){
clearstack(result);
push(result,cur);
while(top>s->b)
push(result,--top);
}
}

void draw(int y,int x)
{
bar(100+15*x,100+15*y,115+15*x,115+15*y);
}

void main(void)
{
int i;
int x,y;
int gd=DETECT,gm;
stack* s=NULL;
stack* result=NULL;
pos* cur=NULL;

initgraph(&gd,&gm,"");
for(x=0;x<10;x++)
for(y=0;y<10;y++){
if(mg[y][x]){
setfillstyle(SOLID_FILL,3);
draw(y,x);
}
}

result=(stack*)malloc(sizeof(stack));
initstack(result);
s=(stack*)malloc(sizeof(stack));
cur=(pos*)malloc(sizeof(pos));
initstack(s);
cur->x=1;
cur->y=0;
cur->dir=0;
push(s,cur);
cur->x=1;
cur->y=1;
cur->dir=1;
do{
if(cur->x==9&&cur->y==8)
savepath(s,cur,result);
step(s,cur,result);
}while(!stackempty(s));
if(stackempty(result))
printf("no way available");
else{
int ch=0;
printf("following is the shortest path:\n");
while(!stackempty(result)){
pop(result,cur);
setfillstyle(SOLID_FILL,15);
draw(cur->y,cur->x);
if(ch>5){
putchar('\n');
ch=0;
}
printf("(%d,%d,%d) ",cur->x,cur->y,cur->dir);
ch++;
}
}
printf("\n");
destroystack(s);
destroystack(result);
free(cur);
printf("Press any key to end");
while(!kbhit());
closegraph();
}

网上copy... 没运行过~``


 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-05-10 21:48
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 

迷宫求解

求迷宫中从入口到出口的所有路径是一个经典的程序设计问题。由于计算机解迷宫时,通常用的是“穷举求解”的方法,即从入口出发,顺某一方向向前探索,若能走通,则继续往前走;否则沿原路退回,换一个方向再继续探索,直至所有可能的通路都探索到为止。为了保证在任何位置上都能沿原路退回,显然需要用一个后进先出的结构来保存从入口到当前位置的路径。因此,在求迷宫通路的算法中应用“栈”也就是自然而然的事了。

首先,在计算机中可以用如图3.4所示的方块图表示迷宫。图中的每个方块或为通道(以蓝方块表示),或为墙(以黄方块表示)。所求路径必须是简单路径,即在求得的路径上不能重复出现同一通道块。

假设“当前位置”指的是“在搜索过程中某一时刻所在图中某个方块位置”,则求迷宫中一条路径的算法的基本思想是:若当前位置“可通”,则纳入“当前路径”,并继续朝“下一位置”探索,即切换“下一位置”为“当前位置”,如此重复直至到达出口;若当前位置“不可通”,则应顺着“来向”退回到“前一通道块”,然后朝着除“来向”之外的其他方向继续探索;若该通道块的四周四个方块均“不可通”,则应从“当前路径”上删除该通道块。
图3.4 迷宫
所谓“下一位置”指的是“当前位置”四周四个方向(东、南、西、北)上相邻的方块。假设以栈S记录“当前路径”,则栈顶中存放的是“当前路径上最后一个通道块”。由此,“纳入路径”的操作即为“当前位置入栈”;“从当前路径上删除前一通道块”的操作即为“出栈”。求迷宫中一条从入口到出口的路径的算法可简单描述如下:

设定当前位置的初值为入口位置;
do{
若当前位置可通,
则{ 将当前位置插入栈顶;
若该位置是出口位置,则结束;//位置求得路径存放在栈中
否则切换当前位置的东邻方块为新的当前位置; }
否则{
若栈不空且栈顶位置尚有其他方向未经探索,
则设定新的当前位置为沿顺时针方向旋转找到的栈顶位置的下一相邻块;
若栈不空但栈顶位置的四周均不可通,
则{ 删去栈顶位置; //从路径中删去该通道块
若栈不空,则重新测试新的栈顶位置,直至找到一个可通的相邻块或出栈至栈空; }
}
}while(栈不空);


 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-05-10 21:49
wizardkid
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-4-14
收藏
得分:0 
回复:(I喜欢c)#include #include&...

谢谢哈

2007-05-11 07:43
wizardkid
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-4-14
收藏
得分:0 
回复:(I喜欢c)#include #include&...
但是有个问题。执行的时候踏提示:fatal error C1021: invalid preprocessor command 'includestdio'
执行 cl.exe 时出错.


怎么回事?
2007-05-12 13:06
快速回复:[求助]一道实验报告题(迷宫求解)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015003 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved