📄 mg.h
字号:
#include"stdio.h"
#include<iostream.h>
#include<stdlib.h>
#define M 11
#define P 11
#define N 100
typedef struct //控制方向用
{
int x;
int y;
int Dir;
}dirtype;
typedef struct //控制访问情况
{
int a;
int b;
}mvtype;
void Inital(int Maze[][P],int Mark[][P],mvtype Move[])//初始化迷官
{
int i,j;
for(i=1;i<M-1;i++) //初始化内部
{
for(j=1;j<P-1;j++)
{
Maze[i][j]=0;
Mark[i][j]=0;
}
}
for(i=0;i<P;i++) //初始化外墙
{
Maze[0][i]=1;
Maze[M-1][i]=1;
}
for(i=0;i<M;i++)
{
Maze[i][0]=1;
Maze[i][P-1]=1;
}
Move[0].a=-1; //移动方向初始化
Move[0].b=0;
Move[4].a=-1;
Move[4].b=1;
Move[1].a=0;
Move[1].b=1;
Move[5].a=1;
Move[5].b=1;
Move[2].a=1;
Move[2].b=0;
Move[6].a=1;
Move[6].b=-1;
Move[3].a=0;
Move[3].b=-1;
Move[7].a=-1;
Move[7].b=-1;
Maze[1][4]=1; //设置障碍
Maze[2][4]=1;
Maze[3][4]=1;
Maze[4][4]=1;
Maze[5][4]=1;
Maze[6][4]=1;
Maze[7][4]=1;
Maze[7][2]=1;
Maze[8][4]=1;
Maze[8][3]=1;
Maze[8][2]=1;
Maze[2][1]=1;
Maze[5][5]=1;
Maze[1][7]=1;
Maze[1][6]=1;
Maze[5][8]=1;
Maze[6][8]=1;
Maze[6][9]=1;
Maze[8][9]=1;
}
void output(int Maze[][P],int Mark[][P],mvtype Move[])//输出当时的行走情况
{
int i,j;
for(i=0;i<P;i++)
{
cout<<" * ";
for(j=0;j<M;j++)
{
if(Maze[i][j]==1&&!(i==0&&j==1)&&!(i==10&&j==9))
{
cout<<"□";
}
if(Maze[i][j]==0&&Mark[i][j]==0)
{
cout<<" ";
}
if(Maze[i][j]==0&&Mark[i][j]==1)
{
cout<<"○";
}
if((i==0&&j==1)||(i==10&&j==9))
{
cout<<"☆";
}
}
cout<<endl;
}
}
void GetAWay(int Maze[][P],int Mark[][P],mvtype Move[])//探测道路
{
dirtype Stack[N],p;
int i,j,d,g,h,Top,cc;
Top=0;
Stack[0].x=1;
Stack[0].y=1;
Mark[1][1]=1;
Stack[0].Dir=0;
while(Top>=0)
{
p=Stack[Top];
Top--;
i=p.x;
j=p.y;
d=p.Dir;
}
while(d<8)
{
cc=0;
while(cc<99999999999999&&cc++);
system("cls");
g=i+Move[d].a;
h=j+Move[d].b;
if((g==M-1)&&(h==P-1)) //到达终点
{
return;
}
if((Maze[g][h]==0)&&(Mark[g][h]==0))
{
Mark[g][h]=1;
p.x=i;
p.y=j;
p.Dir=d+1;
if(Top==N-1)
{
printf("full!\n");
return;
}
else
{
Top++;
Stack[Top]=p;
}
i=g;j=h;d=0;
}
else
{
d=d+1;
}
cout<<" 实验三 "<<endl;
cout<<" **********************************************************"<<endl;
cout<<" * *"<<endl;
cout<<" * 迷官求解,迷宫设置需在程序里控制 *"<<endl;
cout<<" * *"<<endl;
cout<<" **********************************************************"<<endl;
output(Maze,Mark,Move);
}
printf("\n there is not a way\n");
}
void mg_main()
{
int Maze[M][P],Mark[M][P];
mvtype Move[8];
Inital(Maze,Mark,Move);
GetAWay(Maze,Mark,Move);
cout<<"按任意键以退出:"<<endl;
getche();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -