找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1214|回复: 10

[原创]迷宫问题

[复制链接]
发表于 2009-11-2 04:43:50 | 显示全部楼层 |阅读模式 IP:江苏扬州
*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: 风动 E-mail:xidiankeda492@yahoo.com.cn QQ:305687910
*/ 时间: 2007-7-21 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------


#include "iostream"
#include "stdlib.h"
#define STACKZISE 100
#define MAZESIAZE 4
using namespace std;
typedef struct
{
int x;
int y;
}PosType; //定义位置
typedef struct
{
PosType seat;
int sept;
int dirction;
}ElemType; //定义方向位置和走的次数
typedef struct NodeType
{
ElemType data;
NodeType *next;
}NodeType, *LinkType; //定义链表
typedef struct
{
LinkType top;
int size;
}StackType;
typedef struct
{
int col;
int row;
char arr[ STACKZISE+2][ STACKZISE+2];
}MazeType;
void InitStack(StackType &stack)
{
stack.top = NULL;
stack.size = 0;
} // 初始化栈
//进栈
void Push(StackType &stack, ElemType e)
{
LinkType p;
p = (LinkType)malloc(sizeof(NodeType));
p->next = stack.top;
p->data = e;
stack.top = p;
stack.size++;
}
//出栈
void Pop(StackType &stack, ElemType &e)
{
LinkType p;
p = stack.top;
e = p->data;
stack.top = p->next;
free(p);
stack.size--;
}
//初始化迷宫
void InitMaze(MazeType &maze, int arr[MAZESIAZE][MAZESIAZE], int m, int n)
{
maze.col = m+1;
maze.row = n+1;
maze.arr[m+1][n+1] = arr[m][n];
}
//标记不同路径
MazeType Marked(MazeType &maze, PosType curpos)
{
maze.arr[curpos.y][curpos.x] = '@';
return maze;
}
//标记走过路径
MazeType PrintMaze(MazeType &maze, PosType curpos)
{
maze.arr[curpos.y][curpos.x] = '*';
return maze;
}
//判断路径是否可通
bool Pass(MazeType &maze, PosType curpos)
{
if(maze.arr[curpos.y][curpos.x]==1 ||
maze.arr[curpos.y][curpos.x]=='#' || maze.arr[curpos.y][curpos.x]=='@'
||maze.arr[curpos.y][curpos.x]=='*')
return false;
else
return true;
}
//判断是否结束查找
bool Same(PosType curpos,PosType end)
{
if(curpos.x == end.x && curpos.y == end.y)
return true;
else
return false;
}
//下一步的位置
PosType NextPos(PosType &curpos, int dirction)
{
switch (dirction)
{
case 1 : curpos.x++; break;
case 2 : curpos.y++; break;
case 3 : curpos.x--; break;
case 4 : curpos.y--; break;
}
return curpos;
}
//判断是否为空栈
bool StackEmpty(StackType stack)
{
if(stack.top == NULL)
return true;
else
return false;
}
//判断有无路径可通
bool MazePath(MazeType &maze, PosType start, PosType end)
{
int curstep = 1;
ElemType e;
StackType stack;
InitStack(stack);
PosType curpos;
curpos= start;
bool found = false;
do
{
if(Pass(maze,curpos)) // the path pass
{
PrintMaze(maze, curpos);
e.sept = curstep;
e.seat = curpos;
e.dirction = 1;
Push(stack, e);
if(Same(curpos, end))
{
found = true;
return found;
}//if
else
{
curpos = NextPos(curpos, 1);//探索下一步
curstep++;
}//else
}//if
else //the path don't pass
{
if(!StackEmpty(stack))
{
Pop(stack, e);
while(e.dirction == 4 && !StackEmpty(stack))
{
Marked(maze,e.seat);
Pop(stack, e);
curstep--;
}//while
if(e.dirction<4)
{
e.dirction++;
Push(stack, e);
curpos = NextPos(e.seat, e.dirction);
}//if
}//if
}//else
}while(!StackEmpty(stack) && !found);
return found;
}//MazePath
void OutPutMaze(MazeType maze)
{
for( maze.col=1; maze.col<=MAZESIAZE;maze.col++)
{
for( maze.row=1; maze.row<=MAZESIAZE; maze.row++)
cout<<maze.arr[maze.col][maze.row]<<" ";
cout<<'\n';
}
}
void main()
{
int s_col;
int s_row;
int m;
int n;
int arr[MAZESIAZE][MAZESIAZE];
MazeType maze;
PosType start;
PosType end;
cout<<"*******************************************************************************"<<endl;
cout<<"************"<<" 只能读入:0或1 0:表示可通 1:表示不通"<<"******************"<<endl;
cout<<"*************"<<" @:表示走过但不通 *:表示通过的路径 "<<"******************"<<endl;
cout<<"*************"<<" 迷宫规模由: MAZESIAZE 确定可任意改变 "<<"******************"<<endl;
cout<<"*************"<<" 设计迷宫小于定义规模可在多出部分加0或1 "<<"******************"<<endl;
cout<<"*******************************************************************************"<<endl;
for(maze.col=0; maze.col<MAZESIAZE+2;maze.col++)
for(maze.row=0; maze.row<MAZESIAZE+2;maze.row++)
maze.arr[maze.col][maze.row] = '#';
for(m=0; m<MAZESIAZE; m++)
{
for(n=0; n<MAZESIAZE; n++)
{
cout<<"读入迷宫数据:"<<endl;
cin>>arr[m][n];
while(arr[m][n]!=1 && arr[m][n]!=0)
{
cout<<"数据不合法请重输:"<<endl;
cin>>arr[m][n];
}//while
InitMaze(maze, arr,m,n);
}//for
}//for
cout<<"读入迷宫为:"<<endl;
for(int i=0; i< MAZESIAZE;i++)
{
for(int j=0; j< MAZESIAZE;j++)
cout<<arr[i][j]<<" ";
cout<<'\n';
}
cout<<"读入迷宫起始位置:";
cin>>start.x>>start.y;
while(start.x<0 ||start.x>MAZESIAZE || start.y<0 ||start.y>MAZESIAZE)
{
cout<<"输入不合法请重输:";
cin>>start.x>>start.y;
}
cout<<"("<<start.x<<")"<<"("<<start.y<<")"<<endl;
cout<<"出口位置:"<<endl;
cin>>end.x>>end.y;
while(end.x<0 || end.x>MAZESIAZE || end.y<0 || end.y>MAZESIAZE)
{
cout<<"输入不合法请重输:";
cin>>end.x>>end.y;
}
cout<<"("<<end.x<<")"<<"("<<end.y<<")"<<endl;
if(MazePath(maze, start,end))
OutPutMaze(maze);
else
cout<<"the mazepath isn't found"<<endl;
}
发表于 2009-11-2 04:43:54 | 显示全部楼层 IP:江苏扬州
可否把问题写一下, 你解决的是什么样的问题..
回复

使用道具 举报

发表于 2009-11-2 04:43:59 | 显示全部楼层 IP:江苏扬州
同感啊
回复

使用道具 举报

发表于 2009-11-2 04:44:05 | 显示全部楼层 IP:江苏扬州
顶下2楼,没有题目,没有结果,看着有点晕!
回复

使用道具 举报

发表于 2009-11-2 04:44:11 | 显示全部楼层 IP:江苏扬州
不是晕了,是睡着了
回复

使用道具 举报

发表于 2009-11-2 04:44:17 | 显示全部楼层 IP:江苏扬州
关于迷宫路径求解问题,自己可以设计迷宫的障碍,0表示不可通过的路障,1表示可通过的路径。再在迷宫中输入入口,它将自动找到一条可通的路径。
回复

使用道具 举报

发表于 2009-11-2 04:44:21 | 显示全部楼层 IP:江苏扬州
呵呵,我曾经用一个图形库做过,那很形象。第一个完整的大程序呢!
回复

使用道具 举报

发表于 2009-11-2 04:44:27 | 显示全部楼层 IP:江苏扬州
以前用MFC做的:

附件: 只有本站会员才能下载或查看附件,请您 登录 或 注册
回复

使用道具 举报

发表于 2009-11-2 04:44:34 | 显示全部楼层 IP:江苏扬州
斑竹的程序我已经认真拜读过,非常好!谢谢!但如果能把迷宫的起始位置交由用户自己设计不是更好吗?
回复

使用道具 举报

发表于 2009-11-2 04:44:42 | 显示全部楼层 IP:江苏扬州
弱弱的问一下:怎么用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 12:24 , Processed in 0.266298 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表