
帮忙设计一个{数据结构C++}的【迷宫问题】~~谢谢各位!!
{我一共就这么点经验值。各位帮帮忙吧。}【知道上好多类似的。但不全部符合要求。希望一定要能运行昂!谢谢~~】任务六:迷宫问题【任务描述】设计一个计算机自动走迷宫的程序。【...
{我一共就这么点经验值。各位帮帮忙吧。}
【知道上好多类似的。但不全部符合要求。希望一定要能运行昂!谢谢~~】
任务六:迷宫问题
【任务描述】设计一个计算机自动走迷宫的程序。
【基本要求】
(1) 用随机函数设置迷宫;
(2) 选择合适的数据结构表示迷宫。
(3) 迷宫入口处的下标是(x0,y0),出口处的下标是(x1,y1),由键盘输入。
(4) 输出从入口到出口的最短通路(如存在)或不存在通路的信息。
【设计提示】
(1)用一个字符类型的二维数组表示迷宫,数组中的每个元素表示一个小方格,取值“0”(通道)或“1”(阻塞物)。
(2)找一条从迷宫入口到迷宫出口的途径小方格最少的最短通路需要使用队列。 展开
【知道上好多类似的。但不全部符合要求。希望一定要能运行昂!谢谢~~】
任务六:迷宫问题
【任务描述】设计一个计算机自动走迷宫的程序。
【基本要求】
(1) 用随机函数设置迷宫;
(2) 选择合适的数据结构表示迷宫。
(3) 迷宫入口处的下标是(x0,y0),出口处的下标是(x1,y1),由键盘输入。
(4) 输出从入口到出口的最短通路(如存在)或不存在通路的信息。
【设计提示】
(1)用一个字符类型的二维数组表示迷宫,数组中的每个元素表示一个小方格,取值“0”(通道)或“1”(阻塞物)。
(2)找一条从迷宫入口到迷宫出口的途径小方格最少的最短通路需要使用队列。 展开
1个回答
展开全部
#include<stdio.h> ?
#include<conio.h>
#include<bios.h>
#define LEFT 0x4b00 /*光标左键值*/
#define RIGHT 0x4d00/*光标右键值*/
#define DOWN 0x5000/*光标下键值*/
#define UP 0x4800/*光标上键值*/
#define ESC 0x011b/*退出键值*/
typedef struct /*定义一个自定义结构类型*/
{
int x;
int y;
}point; /*定义屏幕中点的结构类型*/
char map[25][50]= /*用二维数组定义一个地图,由于论坛系统缘故,
图形有些乱码,可查看附件;地图可以自己画*/
{
"###################################### #",
"# # ## # # # # # ### ########",
"# # ## # # #### # # # ##",
"# # # ## ## # # ## # # ### #### ###",
"# # #### # # # ## #### ",
"# # # # ## ## ## # # # # # # ##",
"# ## ## # # # # # # # # # ## ",
"# # ## ## # # # ### ## ### ####",
"# # # ## ## # # # # ### ## ###",
"# # # # # # # ## # # ## # # ####",
"# # # # ## # ## # # # ## ##",
"# ## # ## # ### # ##### ## ###",
"# # ##### # ## # # ## # ## ## # # ##",
"# ### ### # # # ## ## ## ##",
"## # # # # ## ### ## # ## ## ### ##",
"## # # ## ## ### # # ## # # ####",
"# # # # # ## ## ## # # #",
"###################################### #",
};
void Drawman(int x,int y)
{
gotoxy(x+5,y+5);
printf("%c\b",2); /*指定位置输出一个小人*/
}
void DrawSpace(int x,int y)
{
gotoxy(x+5,y+5);
printf(" ");
}
void Drawmap()
{
int x=0,y=0;
for(;y<18;y++)
{
for(x=0;x<40;x++)
{
if(map[y][x]=='#')
{
gotoxy(x+5,y+5);
textcolor(BLUE);
putch(219);
}
}
}
gotoxy(30,2);
printf("You must to play the game,please take this step:");
gotoxy(48,8);
printf("1:Press UP,DOWN,LEFT,");
gotoxy(48,9);
printf(" RIGHT to move the man!");
gotoxy(48,11);
printf("2:Press ESC to quit!");
gotoxy(50,22);
printf("by--jiezikonglin");
}
int main()
{
char y_or_n;
char name[10];
int i;
point man={1,1},des={43,5}; /*初始和目的位置,有地图决定*/
int key=0;
textbackground(GREEN);
textcolor(RED);
printf("Your name:");
scanf("%s",&name);
clrscr();
printf("Hello!");
printf("%s",name);
Drawmap();
textcolor(YELLOW);
gotoxy(des.x,des.y);
putch('*');
Drawman(man.x,man.y);
for(;;)
{
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case LEFT:
if(map[man.y][man.x-1]=='#')break;
DrawSpace(man.x,man.y); /*输出一个空格*/
--man.x;
Drawman(man.x,man.y);
break;
case RIGHT:
if(map[man.y][man.x+1]=='#')break;
DrawSpace(man.x,man.y);
++man.x;
Drawman(man.x,man.y);
break;
case DOWN:
if(map[man.y+1][man.x]=='#')break;
DrawSpace(man.x,man.y);
++man.y;
Drawman(man.x,man.y);
break;
case UP:
if(map[man.y-1][man.x]=='#')break;
DrawSpace(man.x,man.y);
--man.y;
Drawman(man.x,man.y);
break;
default: ;
}
if(man.x+5==des.x&&man.y+5==des.y)
{
gotoxy(35,3);
textcolor(YELLOW);
printf("HaHa!You win the game!");
getch();
key=ESC;
}
if(key==ESC)
{
textbackground(0);
textcolor(RED);
clrscr();
printf("Quit the game\n(Y or N)");
scanf("%c",&y_or_n);
if(y_or_n=='Y') return 0;
{printf("press Esc to exit!"); }
if(y_or_n=='N') {scanf("%c",&y_or_n);main();}/*怎样回到之前界面呢????*/
} /* 这样能编译成功,莫非main函数*/
} /* 嵌套调用,哈哈,这样就解决了*/
getch(); /* 但是字变成了红色的了 */
/* 还有问题按下Y or N怎么没用 */ /*去掉else之后要按两下ESC才退出*/
}
这是C语言版的
#include<conio.h>
#include<bios.h>
#define LEFT 0x4b00 /*光标左键值*/
#define RIGHT 0x4d00/*光标右键值*/
#define DOWN 0x5000/*光标下键值*/
#define UP 0x4800/*光标上键值*/
#define ESC 0x011b/*退出键值*/
typedef struct /*定义一个自定义结构类型*/
{
int x;
int y;
}point; /*定义屏幕中点的结构类型*/
char map[25][50]= /*用二维数组定义一个地图,由于论坛系统缘故,
图形有些乱码,可查看附件;地图可以自己画*/
{
"###################################### #",
"# # ## # # # # # ### ########",
"# # ## # # #### # # # ##",
"# # # ## ## # # ## # # ### #### ###",
"# # #### # # # ## #### ",
"# # # # ## ## ## # # # # # # ##",
"# ## ## # # # # # # # # # ## ",
"# # ## ## # # # ### ## ### ####",
"# # # ## ## # # # # ### ## ###",
"# # # # # # # ## # # ## # # ####",
"# # # # ## # ## # # # ## ##",
"# ## # ## # ### # ##### ## ###",
"# # ##### # ## # # ## # ## ## # # ##",
"# ### ### # # # ## ## ## ##",
"## # # # # ## ### ## # ## ## ### ##",
"## # # ## ## ### # # ## # # ####",
"# # # # # ## ## ## # # #",
"###################################### #",
};
void Drawman(int x,int y)
{
gotoxy(x+5,y+5);
printf("%c\b",2); /*指定位置输出一个小人*/
}
void DrawSpace(int x,int y)
{
gotoxy(x+5,y+5);
printf(" ");
}
void Drawmap()
{
int x=0,y=0;
for(;y<18;y++)
{
for(x=0;x<40;x++)
{
if(map[y][x]=='#')
{
gotoxy(x+5,y+5);
textcolor(BLUE);
putch(219);
}
}
}
gotoxy(30,2);
printf("You must to play the game,please take this step:");
gotoxy(48,8);
printf("1:Press UP,DOWN,LEFT,");
gotoxy(48,9);
printf(" RIGHT to move the man!");
gotoxy(48,11);
printf("2:Press ESC to quit!");
gotoxy(50,22);
printf("by--jiezikonglin");
}
int main()
{
char y_or_n;
char name[10];
int i;
point man={1,1},des={43,5}; /*初始和目的位置,有地图决定*/
int key=0;
textbackground(GREEN);
textcolor(RED);
printf("Your name:");
scanf("%s",&name);
clrscr();
printf("Hello!");
printf("%s",name);
Drawmap();
textcolor(YELLOW);
gotoxy(des.x,des.y);
putch('*');
Drawman(man.x,man.y);
for(;;)
{
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case LEFT:
if(map[man.y][man.x-1]=='#')break;
DrawSpace(man.x,man.y); /*输出一个空格*/
--man.x;
Drawman(man.x,man.y);
break;
case RIGHT:
if(map[man.y][man.x+1]=='#')break;
DrawSpace(man.x,man.y);
++man.x;
Drawman(man.x,man.y);
break;
case DOWN:
if(map[man.y+1][man.x]=='#')break;
DrawSpace(man.x,man.y);
++man.y;
Drawman(man.x,man.y);
break;
case UP:
if(map[man.y-1][man.x]=='#')break;
DrawSpace(man.x,man.y);
--man.y;
Drawman(man.x,man.y);
break;
default: ;
}
if(man.x+5==des.x&&man.y+5==des.y)
{
gotoxy(35,3);
textcolor(YELLOW);
printf("HaHa!You win the game!");
getch();
key=ESC;
}
if(key==ESC)
{
textbackground(0);
textcolor(RED);
clrscr();
printf("Quit the game\n(Y or N)");
scanf("%c",&y_or_n);
if(y_or_n=='Y') return 0;
{printf("press Esc to exit!"); }
if(y_or_n=='N') {scanf("%c",&y_or_n);main();}/*怎样回到之前界面呢????*/
} /* 这样能编译成功,莫非main函数*/
} /* 嵌套调用,哈哈,这样就解决了*/
getch(); /* 但是字变成了红色的了 */
/* 还有问题按下Y or N怎么没用 */ /*去掉else之后要按两下ESC才退出*/
}
这是C语言版的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询