| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 790 人关注过本帖
标题:[求助]迷宫在哪里
只看楼主 加入收藏
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
 问题点数:0 回复次数:6 
[求助]迷宫在哪里

由于上个学期没学好数据结构,这几天自学想用C编经典的迷宫题目,一点头绪都没啊~被折腾了几天,哪位兄弟发个上来让小弟参考下啊~

搜索更多相关主题的帖子: 迷宫 经典 兄弟 数据结构 
2006-06-29 00:28
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

这个是由论坛上面的一个C++程序改编过来的,但不知道哪里有错~
还有语句~srand((rand()*(a++)+(b++))%65536);为什么要这样用~?里面的((rand()*(a++)+(b++))%65536是根据什么得来的~?
#include<iostream>
#include<time.h>

using namespace std;

const int MaxX = 11; //最大列数
const int MaxY = 11; //最大行数

struct Point //点
{
int x;
int y;
};


void CLabyrinth(int&,int&,int&); //得到迷宫尺寸
void SetMap(int&,int&,int); //手工设置迷宫结构
void AutoSetMap(int&,int&,int); //自动设置迷宫结构
void ShowMap(int&,int&,int); //显示迷宫
void SearchWay(int&,int&,int&,int&,Point); //搜索通路
void ShowResult(int&,Point); //显示通路

void CLabyrinth(int& X,int& Y,int& top)
{
while(true)
{
cout<<"\t\t迷宫问题\t\t\n"
<<"输入迷宫的大小X*Y(最大为11*11):\n";
int x,y;
cin>>x>>y;
if(x>11||y>11) cout<<"\n\n迷宫最大为11*11\n\n";
else
{
X = (x>9) ? 11 : x+2; //多两个单位是存放迷宫外壁
Y = (y>9) ? 11 : y+2;
top=-1;
}break;
}
}

void SetMap(int& X,int& Y,int Map[][])
{
cout<<"\n依次输入迷宫的结构(1为有障碍,0为无障碍):\n"
<<"前进方向有4个,分别是上、下、左、右\n";
for(int i=0,j,t;i<X;i++)
for(j=0;j<Y;j++)
{
if(i==0 || j==0 || i==X-1 || j==Y-1) //在迷宫周围包上一圈1,防止搜索通路时越界
{
Map[i][j]=1;
continue;
}
cin>>t;
Map[i][j] = (t!=0) ? 1 : 0;
}
}

void AutoSetMap(int& X,int& Y,int Map[][])
{
cout<<"\n前进方向有4个,分别是上、下、左、右\n";
int a=39,b=72;
for(int i=0,j;i<X;i++)
{
srand(time(0));
for(j=0;j<Y;j++)
{
if(i==0 || j==0 || i==X-1 || j==Y-1)
{
Map[i][j]=1;
continue;
}
srand((rand()*(a++)+(b++))%65536);
Map[i][j]=rand()%2;
}
}
}

void ShowMap(int& X,int& Y,int Map[][])
{
cout<<"\n迷宫示意图为:\n";
for(int i=1,j;i<X-1;i++)
{
for(j=1;j<Y-1;j++)
{
if(i==1&&j==1) cout<<"\n入口->";
else cout<<" ";
cout<<Map[i][j];
}
if(i==X-2&&j==Y-1) cout<<"->出口";
cout<<endl;
}
}

void SearchWay(int& X,int& Y,int& top,int Map[][],Point Result[])
{
if(Map[X-2][Y-2]) return; //如果出口不通直接返回
int i=X-2,j=Y-2; //出口的坐标 从出口向入口找路
int pow; //检查方向的计数器
do
{
if(!Map[i][j]) //map[i][j]=0时
{
Result[++top].x = i; //将要可通行的坐标入栈
Result[top].y = j;
if(Result[top].x==1&&Result[top].y==1) break; //如果找到了入口退出循环
i = i-1; //下一个要检查的坐标 上方
if(i==Result[top-1].x&&j==Result[top-1].y)
continue;
else
pow=0; //计数器清零
}
else
{
pow++;

i = Result[top].x; //取出栈顶元素 上一次的可通行坐标
j = Result[top].y;
switch(pow)
{

case 1: j--; //左
if(i==Result[top-1].x && j==Result[top-1].y) //若是来路
pow++;

else
break;

case 2:i++; //下
if(i==Result[top-1].x && j==Result[top-1].y) //若是来路
pow++;

else
break;

case 3:j++; //右
if(i==Result[top-1].x && j==Result[top-1].y)
pow++;

else break;

default: Map[i][j] = 1;i = Result[--top].x;j = Result[top].y; //将该坐标设为不可通行 再回到上一次可通行坐标
}
}
}while(top!=-1); //栈不为空继续循环
}

void ShowResult(int& top,Point Result[])
{
if(top==-1)
{
cout<<"\n此迷宫没有通路!!!\n";
return;
}
cout<<"\n此迷宫的其中一条通路为:\n入口";
for(;top!=-1;top--)
cout<<"->("<<Result[top].x<<','<<Result[top].y<<')';
cout<<"->出口\n";
}

void main()
{

int XX,YY; //迷宫的大小
int MMap[11][11]; //迷宫结构
Point RResult[255]; //存放正确的路径(栈)
int ttop; //栈顶(指针)
CLabyrinth(XX,YY,ttop);
while(1)
{
cout<<"\n1、手工绘制迷宫\n"
<<"2、电脑绘制迷宫(很大几率绘制不出有通路地图)\n";
int usr;
cin>>usr;
if(usr==1) SetMap(XX,YY,MMap);
else AutoSetMap(XX,YY,MMap);
ShowMap(XX,YY,MMap);
SearchWay(XX,YY,ttop,MMap,RResult);
ShowResult(ttop,RResult);
cout<<"再来一次(Y/N)?";
char again;
cin>>again;
if(again!='Y'&&again!='y') break;
}
}

2006-06-29 22:51
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

呵呵~改出来了~
Map[][]改为Map[11][11]就可以了~?不过接收参数要指明下标么~?那么为什么Result[]不需要~?
还有问题就是:语句~srand((rand()*(a++)+(b++))%65536);为什么要这样用~?里面的((rand()*(a++)+(b++))%65536是根据什么得来的~?
版主~解答啊~紧急

2006-06-29 23:01
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

为什么输入迷宫:
0 0 0 0
1 1 1 0
1 1 0 0
1 1 0 1
1 1 0 0
1 1 1 0
会显示没有通路~??

2006-06-29 23:07
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
迷宫是要有墙壁的,你把墙壁变成了通道.当然会没路!

PS:你其他的问题我实在没时间看..最近很忙!

[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-06-29 23:10
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

晕~你真的是没看~那个墙壁是自动初始化的~就是
0 0 0 0
1 1 1 0
1 1 0 0
1 1 0 1
1 1 0 0
1 1 1 0
外面自动加一层1,只是不用自己输入而已
唉~

2006-06-30 07:21
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

修改成这样~不过还是没解决输入上面的迷宫为什么没有结果的问题~
#include<iostream>
#include<time.h>

using namespace std;

const int MaxX = 11; //最大列数
const int MaxY = 11; //最大行数

struct Point //点
{
int x;
int y;
};


void CLabyrinth(int&,int&,int&); //得到迷宫尺寸
void SetMap(int&,int&,int); //手工设置迷宫结构
void AutoSetMap(int&,int&,int); //自动设置迷宫结构
void ShowMap(int&,int&,int); //显示迷宫
void SearchWay(int&,int&,int&,int,Point); //搜索通路
void ShowResult(int&,Point); //显示通路

void CLabyrinth(int& X,int& Y,int& top)
{
while(true)
{
cout<<"\t\t迷宫问题\t\t\n"
<<"输入迷宫的大小X*Y(最大为11*11):\n";
int x,y;
cin>>x>>y;
if(x>11||y>11) cout<<"\n\n迷宫最大为11*11\n\n";
else
{
X = (x>9) ? 11 : x+2; //多两个单位是存放迷宫外壁
Y = (y>9) ? 11 : y+2;
top=-1;
}break;
}
}

void SetMap(int& X,int& Y,int Map[11][11])
{
cout<<"\n依次输入迷宫的结构(1为有障碍,0为无障碍):\n"
<<"前进方向有4个,分别是上、下、左、右\n";
for(int i=0,j,t;i<X;i++)
for(j=0;j<Y;j++)
{
if(i==0 || j==0 || i==X-1 || j==Y-1) //在迷宫周围包上一圈1,防止搜索通路时越界
{
Map[i][j]=1;
continue;
}
cin>>t;
Map[i][j] = (t!=0) ? 1 : 0;
}
}

void AutoSetMap(int& X,int& Y,int Map[11][11])
{
cout<<"\n前进方向有4个,分别是上、下、左、右\n";
int a=39,b=72;
for(int i=0,j;i<X;i++)
{
srand(time(0));
for(j=0;j<Y;j++)
{
if(i==0 || j==0 || i==X-1 || j==Y-1)
{
Map[i][j]=1;
continue;
}
srand((rand()*(a++)+(b++))%65536);
Map[i][j]=rand()%2;
}
}
}

void ShowMap(int& X,int& Y,int Map[11][11])
{
cout<<"\n迷宫示意图为:\n";
for(int i=1,j;i<X-1;i++)
{
for(j=1;j<Y-1;j++)
{
if(i==1&&j==1) cout<<"\n入口->";
else cout<<" ";
cout<<Map[i][j];
}
if(i==X-2&&j==Y-1) cout<<"->出口";
cout<<endl;
}
}

void SearchWay(int& X,int& Y,int& top,int Map[11][11],Point Result[])
{
if(Map[X-2][Y-2]) return; //如果出口不通直接返回
int i=X-2,j=Y-2; //出口的坐标 从出口向入口找路
int pow; //检查方向的计数器
do
{
if(!Map[i][j]) //map[i][j]=0时
{
Result[++top].x = i; //将要可通行的坐标入栈
Result[top].y = j;
if(Result[top].x==1&&Result[top].y==1) break; //如果找到了入口退出循环
i = i-1; //下一个要检查的坐标 上方
if(i==Result[top-1].x&&j==Result[top-1].y)
{
i++;
j--;
pow=1;
}
else
pow=0; //计数器清零
}
else
{
pow++;

i = Result[top].x; //取出栈顶元素 上一次的可通行坐标
j = Result[top].y;
switch(pow)
{

case 1: j--; //左
if(i==Result[top-1].x && j==Result[top-1].y) //若是来路
pow++;

else
break;

case 2:i++; //下
if(i==Result[top-1].x && j==Result[top-1].y) //若是来路
pow++;

else
break;

case 3:j++; //右
if(i==Result[top-1].x && j==Result[top-1].y)
pow++;

else
break;

default: Map[i][j] = 1;i = Result[--top].x;j = Result[top].y; //将该坐标设为不可通行 再回到上一次可通行坐标
}
}
}while(top!=-1); //栈不为空继续循环
}

void ShowResult(int& top,Point Result[])
{
if(top==-1)
{
cout<<"\n此迷宫没有通路!!!\n";
return;
}
cout<<"\n此迷宫的其中一条通路为:\n入口";
for(;top!=-1;top--)
cout<<"->("<<Result[top].x<<','<<Result[top].y<<')';
cout<<"->出口\n";
}

void main()
{

int XX,YY; //迷宫的大小
int MMap[11][11]; //迷宫结构
Point RResult[50]; //存放正确的路径(栈)
int ttop; //栈顶(指针)
CLabyrinth(XX,YY,ttop);
while(1)
{
cout<<"\n1、手工绘制迷宫\n"
<<"2、电脑绘制迷宫(很大几率绘制不出有通路地图)\n";
int usr;
cin>>usr;
if(usr==1) SetMap(XX,YY,MMap);
else AutoSetMap(XX,YY,MMap);
ShowMap(XX,YY,MMap);
SearchWay(XX,YY,ttop,MMap,RResult);
ShowResult(ttop,RResult);
cout<<"再来一次(Y/N)?";
char again;
cin>>again;
if(again!='Y'&&again!='y') break;
}
}

2006-06-30 13:16
快速回复:[求助]迷宫在哪里
数据加载中...
 
   



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

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