返回目录:游戏攻略
#include<iostream>
using namespace std;
class T //定义描述迷宫中当前位置的结构类型
{
public:
int x; //x代表当前位置的行坐标
int y; //y代表当前位置的列坐标
int dir; //0:无效,1:东,2:南,3:西,4:北
};
class LinkNode //链表结点
{
friend class Stack;
public:
T data;
LinkNode *next;
};
class Stack
{
private:
LinkNode *top; //指向第一个结点的栈顶指针
public:
Stack(); //构造函数,置空栈
~Stack(); //析构函数
void Push(T e); //把元素data压入栈中
T Pop(); //使栈顶元素出栈
T GetPop(); //取出栈顶元素
void Clear(); //把栈清空
bool empty(); //判断栈是否为空,如果为空则返回1,否则返回0
};
Stack::Stack() //构造函数,置空栈
{
top=NULL;
}
Stack::~Stack() //析构函数
{
}
void Stack::Push(T e) //把元素x压入栈中
{
LinkNode *P;
P=new LinkNode;
P->data=e;
P->next=top;
top=P;
}
T Stack::Pop() //使栈顶元素出栈
{
T Temp;
LinkNode *P;
P=top;
top=top->next;
Temp=P->data;
delete P;
return Temp;
}
T Stack::GetPop() //取出栈顶元素
{
return top->data;
}
void Stack::Clear() //把栈清空
{
top=NULL;
}
bool Stack::empty() //判断栈是否为空,如果为空则返回1,否则返回0
{
if(top==NULL) return 1;
else return 0;
}
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //定义当前位置移动的4个方向
bool Mazepath(int **maze,int m,int n);
//寻找迷宫maze中从(0,0)到(m,n)的路径
//到则7a64e4b893e5b19e365返回true,否则返回false
void PrintPath(Stack p); //输出迷宫的路径
void Restore(int **maze,int m,int n); //恢复迷宫
int** GetMaze(int &m,int &n); //获取迷宫
//返回存取迷宫的二维指针
int main()
{
int m=0,n=0; //定义迷宫的长和宽
int **maze; //定义二维指针存取迷宫
maze=GetMaze(m,n); //调用GetMaze(int &m,int &n)函数,得到迷宫
if(Mazepath(maze,m,n)) //调用Mazepath(int **maze,int m,int n)函数获取路径
cout<<"迷宫路径探索成功!\n";
else cout<<"路径不存在!\n";
return 0;
}
int** GetMaze(int &m,int &n)//返回存取迷宫的二维指针
{
int **maze; //定义二维指针存取迷宫
int i=0,j=0;
cout<<"请输入迷宫的长和宽:";
int a,b;cin>>a>>b; //输入迷宫的长和宽
cout<<"请输入迷宫内容:\n";
m=a;
n=b; //m,n分别代表迷宫的行数和列数
maze=new int *[m+2]; //申请长度等于行数加2的二级指针
for(i= 0;i<m+2;i++) //申请每个二维指针的空间
{
maze[i]=new int[n+2];
}
for(i=1;i<=m;i++) //输入迷宫的内容,0代表可通,1代表不通
for(j=1;j<=n;j++)
cin>>maze[i][j];
for(i=0;i<m+2;i++)
maze[i][0]=maze[i][n+1]=1;
for(i=0;i<n+2;i++)
maze[0][i]=maze[m+1][i]=1;
return maze; //返回存贮迷宫的二维指针maze
};
bool Mazepath(int **maze,int m,int n)//寻找迷宫maze中从(0,0)到(m,n)的路径
//到则返回true,否则返回false
{
Stack q,p; //定义栈p、q,分别存探索迷宫的过程和存储路径
T Temp1,Temp2;
int x,y,loop;
Temp1.x=1;
Temp1.y=1;
q.Push(Temp1); //将入口位置入栈
p.Push(Temp1);
maze[1][1]=-1; //标志入口位置已到达过
while(!q.empty()) //栈q非空,则反复探索
{
Temp2=q.GetPop(); //获取栈顶元素
if(!(p.GetPop().x==q.GetPop().x&&p.GetPop().y==q.GetPop().y))
p.Push(Temp2);
//如果有新位置入栈,则把上一个探索的位置存入栈p
for(loop=0;loop<4;loop++) //探索当前位置的4个相邻位置
{
x=Temp2.x+move[loop][0]; //计算出新位置x位置值
y=Temp2.y+move[loop][1]; //计算出新位置y位置值
if(maze[x][y]==0) //判断新位置是否可达
{
Temp1.x=x;
Temp1.y=y;
maze[x][y]=-1; //标志新位置已到达过
q.Push(Temp1); //新位置入栈
}
if((x==(m))&&(y==(n))) //成功到达出口
{
Temp1.x=m;
Temp1.y=n;
Temp1.dir=0;
p.Push(Temp1); //把最后一个位置入栈
PrintPath(p); //输出路径
Restore(maze,m,n); //恢复路径
return 1; //表示成功找到路径
}
}
if(p.GetPop().x==q.GetPop().x&&p.GetPop().y==q.GetPop().y)
//如果没有新位置入栈,则返回到上一个位置
{
p.Pop();
q.Pop();
}
}
return 0; //表示查找失败,即迷宫无路经
}
void PrintPath(Stack p) //输出路径
{
cout<<"迷宫的路径为\n";
cout<<"括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向)\n";
Stack t; //定义一个栈,按从入口到出口存取路径
int a,b;
T data;
LinkNode *temp;
temp=new LinkNode; //申请空间
temp->data=p.Pop(); //取栈p的顶点元素,即第一个位置
t.Push(temp->data); //第一个位置入栈t
delete temp; //释放空间
while(!p.empty()) //栈p非空,则反复转移
{
temp=new LinkNode;
temp->data=p.Pop(); //获取下一个位置
//得到行走方向
a=t.GetPop().x-temp->data.x; //行坐标方向
b=t.GetPop().y-temp->data.y; //列坐标方向
if(a==1) temp->data.dir=1; //方向向下,用1表示
else if(b==1) temp->data.dir=2; //方向向右,用2表示
else if(a==-1) temp->data.dir=3; //方向向上,用3表示
else if(b==-1) temp->data.dir=4; //方向向左,用4表示
t.Push(temp->data); //把新位置入栈
delete temp;
}
//输出路径,包括行坐标,列坐标,下一个位置方向
while(!t.empty()) //栈非空,继续输出
{
data=t.Pop();
cout<<'('<<data.x<<','<<data.y<<','<<data.dir<<","; //输出行坐标,列坐标
switch(data.dir) //输出相应的方向
{
case 1:cout<<"↓)\n";break;
case 2:cout<<"→)\n";break;
case 3:cout<<"↑)\n";break;
case 4:cout<<"←)\n";break;
case 0:cout<<")\n";break;
}
}
}
void Restore(int **maze,int m,int n) //恢复迷宫
{
int i,j;
for(i=0;i<m+2;i++) //遍历指针
for(j=0;j<n+2;j++)
{
if(maze[i][j]==-1) //恢复探索过位置,即把-1恢复为0
maze[i][j]=0;
}
}
示例输出:
测试1:
请输入迷宫的长和宽:5 5
请输入迷宫内容:
0 1 1 0 0
0 0 1 1 0
1 0 0 1 1
1 0 0 1 0
1 1 0 0 0
迷宫的路径为
括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向)
(1,1,1,↓)
(2,1,2,→)
(2,2,1,↓)
(3,2,1,↓)
(4,2,2,→)
(4,3,1,↓)
(5,3,2,→)
(5,4,2,→)
(5,5,0,)
迷宫路径探索成功!
测试2:
请输入迷宫的长和宽:9 8
请输入迷宫内容:
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 0 0 1 1 0 1
0 1 1 1 0 0 1 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 1
0 1 1 1 1 0 0 1
1 1 0 0 0 1 0 1
1 1 0 0 0 0 0 0
迷宫的路径为
括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向)
(1,1,1,↓)
(2,1,1,↓)
(3,1,1,↓)
(4,1,1,↓)
(5,1,2,→)
(5,2,2,→)
(5,3,1,↓)
(6,3,2,→)
(6,4,2,→)
(6,5,3,↑)
(5,5,2,→)
(5,6,2,→)
(5,7,1,↓)
(6,7,1,↓)
(7,7,1,↓)
(8,7,1,↓)
(9,7,2,→)
(9,8,0,)
迷宫路径探索成功!
#include<iostream>
using namespace std;
class T//定义描述迷宫中当前位置的结构类型
{
public:
int x;//x代表当前位置的行坐标
int y;//y代表当前位置的列坐标
int dir;//0:无效,1:东,2:南,3:西,4:北
};
class LinkNode//链表结点
{
friend class Stack;
public:
T data;
LinkNode *next;
};
class Stack
{
private:
LinkNode *top;//指向第一个结点的栈e799bee5baa6e58685e5aeb9336顶指针
public:
Stack();//构造函数,置空栈
~Stack()//析构函数
{}
void Push(T e);//元素data入栈中
T Pop();//栈顶元素出栈
T GetPop();//取出栈顶元素
void Clear();//把栈清空
bool empty();//判断栈是否为空,如果为空则返回1,否则返回0
};
Stack::Stack()//构造函数,置空栈
{
top=NULL;
}
void Stack::Push(T e)//元素x入栈中
{
LinkNode *P;
P=new LinkNode;
P->data=e;
P->next=top;
top=P;
}
T Stack::Pop()//栈顶元素出栈
{
T Temp;
LinkNode *P;
P=top;
top=top->next;
Temp=P->data;
delete P;
return Temp;
}
T Stack::GetPop()//取出栈顶元素
{
return top->data;
}
void Stack::Clear()//把栈清空
{
top=NULL;
}
bool Stack::empty()//判断栈是否为空,如果为空则返回1,否则返回0
{
if(top==NULL) return 1;
else return 0;
}
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//定义当前位置移动的4个方向
void PrintPath(Stack p)//输出路径
{
cout<<"迷宫的路径为\n";
cout<<"括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向)\n";
Stack t;//定义一个栈,按从入口到出口存取路径
int a,b;
T data;
LinkNode *temp;
temp=new LinkNode;//获取空间
temp->data=p.Pop();//取栈p的顶点元素,即第一个位置
t.Push(temp->data);//第一个位置入栈t
delete temp;//释放空间
while(!p.empty())//如果栈p非空,则反复转移
{
temp=new LinkNode;
temp->data=p.Pop();//获取下一个位置
//得到行走方向
a=t.GetPop().x-temp->data.x;//行坐标方向
b=t.GetPop().y-temp->data.y;//列坐标方向
if(a==1) temp->data.dir=1;//方向向下,用1表示
else if(b==1) temp->data.dir=2;//方向向右,用2表示
else if(a==-1) temp->data.dir=3;//方向向上,用3表示
else if(b==-1) temp->data.dir=4;//方向向左,用4表示
t.Push(temp->data);//把新位置入栈
delete temp;
}//输出路径,包括行坐标,列坐标,下一个位置方向
while(!t.empty())//栈非空,继续输出
{
data=t.Pop();
cout<<'('<<data.x<<','<<data.y<<','<<data.dir<<",";//输出行坐标,列坐标
switch(data.dir)//输出相应的方向
{
case 1:cout<<"↓)\n";break;
case 2:cout<<"→)\n";break;
case 3:cout<<"↑)\n";break;
case 4:cout<<"←)\n";break;
case 0:cout<<")\n";break;
}
}
}
void Restore(int **maze,int m,int n)//恢复迷宫
{
int i,j;
for(i=0;i<m+2;i++)//遍历指针
for(j=0;j<n+2;j++)
{
if(maze[i][j]==-1)//恢复探索过位置,即把-1恢复为0
maze[i][j]=0;
}
}
int** GetMaze(int &m,int &n)//返回存取迷宫的二维指针
{
int **maze;//定义二维指针存取迷宫
int i=0,j=0;
cout<<"请输入迷宫的长和宽:";
int a,b;cin>>a>>b;//输入迷宫的长和宽
cout<<"请输入迷宫内容:(0为通路,1为墙)\n";
m=a;
n=b;//m,n分别代表迷宫的行数和列数
maze=new int *[m+2];//获取长度等于行数加2的二级指针
for(i= 0;i<m+2;i++)//每个二维指针的空间
{
maze[i]=new int[n+2];
}
for(i=1;i<=m;i++)//输入迷宫的内容,0代表可通,1代表不通
for(j=1;j<=n;j++)
cin>>maze[i][j];
for(i=0;i<m+2;i++)
maze[i][0]=maze[i][n+1]=1;
for(i=0;i<n+2;i++)
maze[0][i]=maze[m+1][i]=1;
return maze;//返回存贮迷宫的二维指针maze
};
bool Mazepath(int **maze,int m,int n)//寻找迷宫maze中从(0,0)到(m,n)的路径
{
Stack q,p;//定义栈p、q,分别存探索迷宫的过程和存储路径
T Temp1,Temp2;
int x,y,loop;
Temp1.x=1;
Temp1.y=1;
q.Push(Temp1);//将入口位置入栈
p.Push(Temp1);
maze[1][1]=-1;//标志入口位置已到达过
while(!q.empty())//栈q非空,则反复探索
{
Temp2=q.GetPop();//获取栈顶元素
if(!(((p.GetPop().x)==(q.GetPop().x))&&((p.GetPop().y)==(q.GetPop().y))))
p.Push(Temp2);
//如果有新位置入栈,则把上一个探索的位置存入栈p
for(loop=0;loop<4;loop++)//探索当前位置的4个相邻位置
{
x=Temp2.x+move[loop][0];//计算出新位置x位置值
y=Temp2.y+move[loop][1];//计算出新位置y位置值
if(maze[x][y]==0)//判断新位置是否可达
{
Temp1.x=x;
Temp1.y=y;
maze[x][y]=-1;//标志新位置已到达过
q.Push(Temp1);//新位置入栈
}
if((x==(m))&&(y==(n)))//成功到达出口
{
Temp1.x=m;
Temp1.y=n;
Temp1.dir=0;
p.Push(Temp1);//把最后一个位置入栈
PrintPath(p);//输出路径
Restore(maze,m,n);//恢复路径
return 1;//表示成功找到路径
}
}
if(p.GetPop().x==q.GetPop().x&&p.GetPop().y==q.GetPop().y)//如果没有新位置入栈,则返回到上一个位置
{
p.Pop();
q.Pop();
}
}
return 0;//表示查找失败,即迷宫无路经
}
int main()
{
int m=0,n=0;//定义迷宫的长和宽
int **maze;//定义二维指针存取迷宫
maze=GetMaze(m,n);//调用GetMaze(int &m,int &n)函数,得到迷宫
if(Mazepath(maze,m,n))//调用Mazepath(int **maze,int m,int n)函数获取路径
cout<<"迷宫路径探索成功!\n";
else cout<<"路径不存在!\n";
return 0;
}
输入数据:
搜索的路径为:
已发
我将e68a84e799bee5baa6339那个有错的C语言版的给改好了,没时间做C++版本的了:
#include<conio.h>
#include<stdio.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
int normol_map[][16]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},//16*16
{1,5,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1},
{1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1},
{1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,1},
{1,0,1,1,1,0,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,1,0,0,1,0,1,0,0,0,1},
{1,1,1,0,1,0,1,1,1,1,0,1,0,1,1,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,1},
{1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,1},
{1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,1},
{1,0,1,1,1,0,0,1,1,0,1,1,1,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,1,0,9,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
char normol_ch,ch_f2=0;
int flag_f5=1;
void Normol_map()
{
system("cls");
cout<<endl<<endl;
cout<<endl<<endl;
for(int i=0;i<16;i++)
{
cout<<"\t"<<"\t";
for(int j=0;j<16;j++)
{
if(normol_map[j]==0)
{
cout<<" ";
}
else if(*normol_map[j]==1)
{
int s,c;
textbackground(0);
textcolor(3);
cputs("\xde");
gotoxy(wherex()-1,wherey());
textbackground(2);
s=rand()%15;
if(s==2)
s++;
c=rand()%5+1;
textcolor(s);
if(c==1)
cputs("\x3");
else if(c==2)
cputs("\x3");
else if(c==3)
cputs("\x3");
else if(c==4)
cputs("\x4");
else if(c==5)
cputs("\x5");
}
else if(*normol_map[j]==5)
{
cout<<char(2);
textbackground(0);
textcolor(2);
}
else if(*normol_map[j]==9)
{
textattr(YELLOW|BLINK|RED*16);
cputs("\xf");
}
}
if(i<16) cout<<endl;
}
gotoxy(18,6);
}
void up_normol()
{
if(normol_map[wherey()-6][wherex()-17]==1);
else
{
if(flag_f5)
{
sound(1000);
delay(100);
nosound();
}
putch(0);
gotoxy(wherex()-1,wherey());
gotoxy(wherex(),wherey()-1);
putch(2);
textbackground(0);
textcolor(2);
gotoxy(wherex()-1,wherey());
}
}
void down_normol()
{
if(normol_map[wherey()-4][wherex()-17]==1);
else
{
if(flag_f5)
{
sound(1500);
delay(100);
nosound();
}
putch(0);
gotoxy(wherex()-1,wherey());
gotoxy(wherex(),wherey()+1);
putch(2);
textbackground(0);
textcolor(2);
gotoxy(wherex()-1,wherey());
if(normol_map[wherey()-5][wherex()-17]==9){system("cls");cout<<"nihao";}
}
}
void left_normol()
{
if(normol_map[wherey()-5][wherex()-18]==1);
else
{
if(flag_f5)
{
sound(300);
delay(100);
nosound();
}
putch(0);
gotoxy(wherex()-1,wherey());
gotoxy(wherex()-1,wherey());
putch(2);
textbackground(0);
textcolor(2);
gotoxy(wherex()-1,wherey());
}
}
void right_normol()
{
if(normol_map[wherey()-5][wherex()-16]==1);
else
{
if(flag_f5)
{
sound(2000);
delay(100);
nosound();
}
putch(0);
gotoxy(wherex()-1,wherey());
gotoxy(wherex()+1,wherey());
putch(2);
textbackground(0);
textcolor(2);
gotoxy(wherex()-1,wherey());
if(normol_map[wherey()-5][wherex()-17]==9){system("cls");cout<<"nihao";}
}
}
void normol_start()
{
Normol_map();
do{
normol_ch=getch();
if(ch_f2=='2'||ch_f2==0)
{
switch(normol_ch)
{
case 119:up_normol();break;
case 115:down_normol();break;
case 100:right_normol();break;
case 97:left_normol();break;
}
}
else if(ch_f2=='1')
{
switch(normol_ch){
case 72:up_normol();break;
case 80:down_normol();break;
case 75:left_normol();break;
case 77:right_normol();break;
}
}
}while(normol_ch!='q');
}
void main()
{
normol_start();
getch();
}