#include <stdio.h>
#include<stdlib.h>
#include <windows.h>
#include<conio.h>
#include<time.h>
#define MAXQSIZE 100 //最大长度
#define OVERFLOW -2
#define OK 1
#define ERROR 0
const int N=1001;//定义地图的大小
int m,n;//定义地图的大小
int x1,x2,y1,y2;//定义起点与终点
int q[N][N],p[N][N];//定义一个二维数组,用邻接矩阵法存储地图
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//定义四个前进的方向
int head,tail;
int t1,t2=0;
bool book[N][N];//标记数组
void color(short x) //自定义函根据参数改变颜色
{
if(x>=0 && x<=15)//参数在0-15的范围颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色
else//默认的颜色白色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
struct note
{
int x;//横坐标
int y;//纵坐标
int s;//步数
int f;//记录走过的路径
};
struct note queue[2501];
void ZTYS()//字体颜色
{
printf("请输入你要选择的字体颜色:\n");
printf("1.淡黄色\n");
printf("2.亮白色\n");
printf("3.淡绿色\n");
printf("4.淡红色\n");
printf("5.淡紫色\n");
printf("6.黑色\n");
printf("7.蓝色\n");
printf("8.绿色\n");
printf("9.红色\n");
printf("10.白色\n");
int z;
scanf("%d",&z);
printf("更改成功!\n");
system("pause");
if(z==1)
{
system("color E");
}
if(z==2)
system("color F");
if(z==3)
system("color A");
if(z==4)
system("color C");
if(z==5)
system("color D");
if(z==6)
system("color 0");
if(z==7)
system("color 1");
if(z==8)
system("color 2");
if(z==9)
system("color 4");
if(z==10)
system("color 7");
}
void YSZY()//页面皮肤设置主页
{
printf("------------请输入选项!----------\n");
printf("------------1.字体颜色 ----------\n");
printf("------------0.退出 ----------\n");
int z;
scanf("%d",&z);
if(z==1)
{
system("CLS");
ZTYS();
}
}
void bfs()
{
head=1;
tail=1;
queue[tail].x=x1;
queue[tail].y=y1;
queue[tail].f=0;
queue[tail].s=0;
tail++;
book[x1][y1]=true;
int flag=0;
while(head<tail)
{
int tx,ty,i;
for(i=0;i<4;i++)
{
tx=queue[head].x+next[i][0];
ty=queue[head].y+next[i][1];
if(tx<1||tx>m||ty<1||ty>n)
continue;
if(q[tx][ty]==0&&book[tx][ty]==false)
{
book[tx][ty]=true;
queue[tail].x=tx;
queue[tail].y=ty;
queue[tail].f=head;
queue[tail].s=queue[head].s+1;
tail++;
}
if(tx==x2&&ty==y2)
{
flag=1;
break;
}
}
if(flag==1)
break;
head++;
}
if(flag==0)
t1=-1;
else
t1=queue[tail-1].s;
}
void input()//输入函数
{
printf("请输入地图的大小(x y):");
scanf("%d %d",&m,&n);//输入地图的大小
printf("请输入地图用0/1表示(0表示路,1表示墙)\n");//输入地图用0/1表示
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
scanf("%d",&q[i][j]);
}
void EnterStart()//输入迷宫的入口
{
printf("请输入迷宫的入口(横坐标 纵坐标):");
scanf("%d %d",&x1,&y1);
}
void EnterEnd()//输入地图的终点
{
printf("请输入地图的终点(横坐标 纵坐标):");
scanf("%d %d",&x2,&y2);
}
void print1()//输出步数的函数
{
if(t1>=0)
{
printf("最短步数为: ");
printf("%d 步",t1);
}
else
printf("走不到终点!!!");
}
void print2()
{
tail--;
int i=0;
while(queue[tail].f!=0)
{
p[i][0]=queue[tail].x;
p[i][1]=queue[tail].y;
tail=queue[tail].f;
i++;
}
p[i][0]=x1;
p[i][1]=y1;
if(t1<0)
printf("走不到终点!!!");
else
{
printf("<%d,%d>",p[t1][0],p[t1][1]);
for(int j=t1-1;j>=0;j--)
printf("-><%d,%d>",p[j][0],p[j][1]);
}
}
void print3()//平面图展示路径
{
int k;
for(k=0;k<=t1;k++)
q[p[k][0]][p[k][1]]=2;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(q[i][j]==0)
{
color(1);
printf("*");
}
else if(q[i][j]==1)
{
color(1);
printf("#");
}
else
{
color(2);
printf("*");
}
color(1);
}
printf("\n");
}
color(16);
}
//以下为动态迷宫
void easy()//简单模式
{
system("CLS");
printf("游戏说明:\n该游戏控制的方向如下:w(上)s(下)a(左)d(右)。\n");
printf("左上角为迷宫的入口,右下角为出口\n");
system("pause");
time_t start_time = time(NULL);
system("CLS");
char h1[200][200] = {
"############################",//21 28
"o ### ## ### ## ###########",
"## #### #########",
"### #### ## ###### ### ##",
"### ### ######### ### ## ##",
"### ###### ### ## ##",
"### ## #### ###### ## ##",
"# ## #### ########### ##",
"# #### ## ###### ##",
"### ### ## ##### #######",
"### ###### ### # ##",
"# ###### #### ###### ##",
"# # ###### ####### #### # ##",
"# # ## ## #### # ##",
"# ##### ## #### ## ##",
"# ## ## #### ###### # ##",
"#### ## ## ## #### # ##",
"# ## ## ## ## # # #",
"# ##### ## ##### ## # # ####",
"# # ###### # # ",
"############################",
};
int x=1,y=0;
char s;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h1[i]);
while(1)
{
s=_getch();
if(s=='w'&&h1[x-1][y]==' '&&x-1>=0)
{
h1[x][y]=' ';
h1[x-1][y]='o';
x--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h1[i]);
}
if(s=='s'&&h1[x+1][y]==' '&&x+1<=20)
{
h1[x][y]=' ';
h1[x+1][y]='o';
x++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h1[i]);
}
if(s=='a'&&h1[x][y-1]==' '&&y1>=0)
{
h1[x][y]=' ';
h1[x][y-1]='o';
y--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h1[i]);
}
if(s=='d'&&h1[x][y+1]==' '&&y+1<=27)
{
h1[x][y]=' ';
h1[x][y+1]='o';
y++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h1[i]);
}
if(x==19&&y==27&&s=='d')
{
system("CLS");
time_t end_time=time(NULL);
printf("-------恭喜你走出了迷宫------\n");
printf("游戏结束!用时%ds\n",end_time-start_time);
Sleep(2000);
break;
}
}
}
void difficult()//困难模式
{
system("CLS");
printf("游戏说明:\n该游戏控制的方向如下:w(上)s(下)a(左)d(右)。\n");
printf("左上角为迷宫的入口,右下角为出口\n");
system("pause");
time_t start_time = time(NULL);
system("CLS");
char h2[200][200] = {
"############################################################################",// 21 76
"o # ## ### # #### ##### ###### #### ####### ##### #### ## ### ######",
"## ## #### # ### # ##### #### ## ### #######",
"##### ## ### ### ##### ### ## ## ### # ## ####### ##### #### ## ### ### #",
"## # ### ##### ### ## ## ### # ## ## #### ## ### ### ##",
"## ## ## # # ### ##### # #### ## ### # # ## #### ### # ## ## ### ### ###",
"##### ## # # ##### ### ## ## # ## ## ## ## ## ### ## ### ###",
"## ## ## ### # # ## # ## ### #### ## #### ### #### ## # # ### ###",
"## ## ## ### # # ## ## ### # #### ### ## #### ## # # ####",
"## ##### ### # # ## ## # # ###### #### # ### ## #### ## # #####",
"## ## ## ### # ## # ## ###### #### ####### # # # #######",
"## ## ## # ## # # ### ## ## # ## # #### ####### # ### # ## # # #####",
"## ##### ### ### # #### ### # ## # # #### ## ## ### ## ## # # # #####",
"## ## ## ### ### ##### ### # ## # # #### ### ### ##### # # # ## # # #######",
"## ## ## ### # ## ##### # # ## # # #### ## ## #### # ## ## ####",
"## ## ## ### ## ##### ## ## ## # # ### ## #### #### #### ##",
"## ## ## ## # ## ##### ## ## ## # # ## ## ## ##### ##### #####",
"## ##### ### ### ##### ## ## ## # # #### ##### # # ## ### # ##",
"## ## ###### ### ## ## ## ## # # #### ## ## # # ## ## # # ##",
"## ## ## ### ### ## ## # ## # #### #### ## ## ## # ## ## ##### ",
"############################################################################",
};
for(int i=0;i<21;i++)
printf("%s\n",h2[i]);
char s;
int x=1,y=0;
while(1)
{
s=_getch();
if(s=='w'&&h2[x-1][y]==' '&&x-1>=0)
{
h2[x][y]=' ';
h2[x-1][y]='o';
x--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h2[i]);
}
if(s=='s'&&h2[x+1][y]==' '&&x+1<=20)
{
h2[x][y]=' ';
h2[x+1][y]='o';
x++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h2[i]);
}
if(s=='a'&&h2[x][y-1]==' '&&y1>=0)
{
h2[x][y]=' ';
h2[x][y-1]='o';
y--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h2[i]);
}
if(s=='d'&&h2[x][y+1]==' '&&y+1<=75)
{
h2[x][y]=' ';
h2[x][y+1]='o';
y++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h2[i]);
}
if(x==19&&y==75&&s=='d')
{
system("CLS");
time_t end_time=time(NULL);
printf("-------恭喜你走出了迷宫------\n");
printf("游戏结束!用时%ds\n",end_time-start_time);
Sleep(2000);
break;
}
}
}
void Mode()//地狱模式
{
system("CLS");
printf("游戏说明:\n该游戏控制的方向如下:w(上)s(下)a(左)d(右)。\n");
printf("左上角为迷宫的入口,右下角为出口\n");
system("pause");
time_t start_time = time(NULL);
system("CLS");
char h3[200][200] = {
"############################################################################",// 21 76
"o # ## ### # #### ##### ###### #### ####### ##### #### ## ### ######",
"## ## #### # ### # ##### #### ## ### #######",//地狱模式
"##### ## ### ### ##### ### ## ## ### # ## ####### ##### #### ## ### ### #",
"## # ### ##### ### ## ## ### # ## ## #### ## ### ### ##",
"## ## ## # # ### ##### # #### ## ### # # ## #### ### # ## ## ### ### ###",
"##### ## # # ##### ### ## ## # ## ## ## ## ## ### ## ### ###",
"## ## ## ### # # ## # ## ######## ## #### ### #### ## # # ### ###",
"## ## ## ### # # ## ## ### # # #### ### ## ### # # ####",
"## ##### ### # # ## ## # ##### #### ### ## ### #### #####",
"## ## ## ### # ## # ## # ## # ## ######## # ### # #######",
"## ## ## # ## # # ### ## # # ## # #### ####### # ### # #### # #####",
"## ##### ### ### # #### ### # # # # #### ## ### ## ## # # # #####",
"## ## ## ### ### ## ## ### # ## # # # ## # # # # ##### # # ## # #########",
"## ## ## ### # ## ### # ## # # #### ## ## #### # ## ### ####",
"## ## ## ### ## ### # # ## # # # ### ## # ## #### #### ##",
"## ## ## ## # ## # ## ## ## ## # # ## ## ## ##### ##### #####",
"## ##### ### ### ## ## # ## ## # # #### ### ## ### # ##",
"## ## ###### ### ## ## ## # # # #### # ## # # ## ## # # ##",
"## ## ## ### ### ## ## ## #### #### ## ## ## # ## ## ##### ",
"############################################################################",
};
for(int i=0;i<21;i++)
printf("%s\n",h3[i]);
char s;
int x=1,y=0;
while(1)
{
s=_getch();
if(s=='w'&&h3[x-1][y]==' '&&x-1>=0)
{
h3[x][y]=' ';
h3[x-1][y]='o';
x--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h3[i]);
}
if(s=='s'&&h3[x+1][y]==' '&&x+1<=20)
{
h3[x][y]=' ';
h3[x+1][y]='o';
x++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h3[i]);
}
if(s=='a'&&h3[x][y-1]==' '&&y1>=0)
{
h3[x][y]=' ';
h3[x][y-1]='o';
y--;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h3[i]);
}
if(s=='d'&&h3[x][y+1]==' '&&y+1<=75)
{
h3[x][y]=' ';
h3[x][y+1]='o';
y++;
system("CLS");
for(int i=0;i<21;i++)
printf("%s\n",h3[i]);
}
if(x==19&&y==75&&s=='d')
{
system("CLS");
time_t end_time=time(NULL);
printf("-------恭喜你走出了迷宫------\n");
printf("游戏结束!用时%ds\n",end_time-start_time);
Sleep(2000);
break;
}
}
}
void DTMG_ZY()//动态迷宫主页
{
printf("-------欢迎来到迷宫小游戏------\n");
printf("------------版本1.0------------\n");
printf("说明:本游戏要时间加载,请耐心等待!\n");
Sleep(2000);
system("cls");
printf("抵制不良游戏,做优秀的升达学子!\n");
printf("作者:刘林宗 ,计算机科学与技术专业\n");
Sleep(2000);
system("cls");
printf("等待游戏中………………\n");
printf("游戏说明:抵制不良游戏,做优秀的升达学子!\n");
printf("游戏说明:用户可以根据自己的能力自行选择难度\n");
Sleep(2000);
printf("------------请输入选项!----------\n");
printf("------------1-简单模式------------\n");
printf("------------2-困难模式------------\n");
printf("------------3-地狱模式------------\n");
printf(" …… \n");
printf("------------0-退出游戏------------\n");
int o;
scanf("%d",&o);
if(o==1)
easy();
if(o==2)
difficult();
if(o==3)
Mode();
}
void initislize()//初始化
{
printf(" **************************************************************\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * 1.输入地图 *\n");
printf(" * 2.输入迷宫的入口 *\n");
printf(" * 3.输入迷宫的出口 *\n");
printf(" * 4.输出从入口到出口最少需要多少步 *\n");
printf(" * 5.输出走迷宫的路径 *\n");
printf(" * 6.用平面图展示路径 *\n");
printf(" * 7.*动态迷宫(游戏)* *\n");
printf(" * 8.页面皮肤设置 *\n");
printf(" * 9.结束 *\n");
printf(" * *\n");
printf(" * *\n");
printf(" **************************************************************\n");
printf(" 请输入选择(1~9):");
int n=7;
scanf("%d",&n);
if(n==1)
{
input();
system("CLS");
initislize();
}
if(n==2)
{
EnterStart();
system("CLS");
initislize();
}
if(n==3)
{
EnterEnd();
system("CLS");
initislize();
}
if(n==4)
{
bfs();
t2=1;
print1();
printf("\n");
system("pause");
system("CLS");
initislize();
}
if(n==5)
{
if(t2==0)
bfs();
print2();
printf("\n");
system("pause");
system("CLS");
initislize();
}
if(n==6)
{
print3();
printf("\n");
system("pause");
system("CLS");
initislize();
}
if(n==7)
{
system("CLS");
DTMG_ZY();
system("CLS");
initislize();
}
if(n==8)
{
system("CLS");
YSZY();
system("CLS");
initislize();
}
}
int main()
{
initislize();
return 0;
}
迷宫问题(c++)
最新推荐文章于 2023-12-29 21:52:34 发布