首页 技术 正文
技术 2022年11月17日
0 收藏 709 点赞 3,853 浏览 1205 个字
 1 #include<bits/stdc++.h>
2 #include<conio.h>
3 using namespace std;
4 char mg[17][17]={
5 "# ##############",
6 "# # # # # ###",
7 "## # # ### ##",
8 "### ### ##",
9 "######## ###",
10 "### ## ###",
11 "### # # # # ##",
12 "### # ### ####",
13 "#### ## # # ##",
14 "### ## ##### ##",
15 "### # # ##",
16 "##### ### ###",
17 "## # ##########",
18 "## #####",
19 "########## #",
20 "############## #"
21 };
22 double start,end;
23 int x=0;int y=1;
24 void disp(){
25 system("cls");
26 for(int i=0;i<16;i++){
27 for(int j=0;j<16;j++){
28 printf("%c",mg[i][j]);
29 }
30 printf("\n");
31 }
32 printf("x..%d,y..%d",x,y);
33 }
34 void win(){
35 end=clock();
36 system("cls");
37 printf("YOU WIN!\nTIME:%.2f SECONDS",((double)(end-start)/CLK_TCK));
38 }
39 int main(){
40 start=clock();
41 mg[x][y]='o';
42 char temp;
43 while(x!=15||y!=14){
44 disp();
45 temp=getch();
46 if(temp=='W'){
47 if(x!=0&&mg[x-1][y]!='#'){
48 mg[x][y]=' ';--x;mg[x][y]='o';
49 }
50 }
51 if(temp=='A'){
52 if(y!=0&&mg[x][y-1]!='#'){
53 mg[x][y]=' ';--y;mg[x][y]='o';
54 }
55 }
56 if(temp=='S'){
57 if(x!=15&&mg[x+1][y]!='#'){
58 mg[x][y]=' ';++x;mg[x][y]='o';
59 }
60 }
61 if(temp=='D'){
62 if(y!=15&&mg[x][y+1]!='#'){
63 mg[x][y]=' ';++y;mg[x][y]='o';
64 }
65 }
66 }
67 win();
68 }

解释:
把迷宫保存在数组里。windows.h和conio.h都是常用的头文件,getch()表示输入一个字符但是不在屏幕上显示这个字符。我们不断读入字符,根据WASD分别判断是往上、下、左、右走,先判断要走的方向是否是墙(用#号表示)​,如果不是,那么就移动,直到终点结束循环。
运行效果截图:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905