编程语言:c++
代码如下:
main.cpp
1 #include <iostream> 2 #include <stack> 3 #include <stdio.h> 4 #include <windows.h> 5 #include "show.cpp" 6 #define N 100 7 #include "jiegou.cpp" 8 #include "shuru.cpp" 9 #include "start.cpp" 10 using namespace std; 11 12 int main() 13 { system("color 0D"); 14 see1(); 15 int x; 16 while(1) 17 { 18 see2(); 19 cin>>x; 20 getchar(); 21 if(x==1) 22 { system("cls"); 23 shuru(); 24 getchar(); 25 cout<<endl<<endl; 26 cout<<"--按任意返回进行其他操作"<<endl; 27 getchar(); 28 system("cls"); 29 continue; 30 } 31 else if(x==2) 32 { 33 cout<<"出迷宫的路线为:"<<endl<<endl; 34 lu(); 35 cout<<endl<<endl; 36 cout<<"--按任意返回进行其他操作"<<endl; 37 getchar(); 38 system("cls"); 39 continue; 40 } 41 else if(x==3) 42 { 43 cout<<"好勒,地图在这里:"<<endl<<endl; 44 tu(); 45 cout<<endl<<endl; 46 cout<<"--按任意返回进行其他操作"<<endl; 47 getchar(); 48 system("cls"); 49 continue; 50 } 51 else if(x==4) 52 { 53 system("cls"); 54 cout<<"再来玩哦"<<endl; 55 Sleep(100); 56 break; 57 } 58 else 59 { 60 cout<<endl<<endl; 61 cout<<"输入选项错误"<<n<<endl<<endl; 62 cout<<"--按任意键返回重新输入"<<endl; 63 getchar(); 64 system("cls"); 65 } 66 67 } 68 69 }
shuru.cpp
1 void shuru() 2 { 3 cout<<endl<<endl<<endl<<endl; 4 cout<<"请输入迷宫的大小:"; 5 cin>>n>>m; 6 getchar(); 7 cout<<"请输入迷宫,字符‘1’代表通路,字符‘0’代表不通,中间无空格"<<endl; 8 for(int i=0;i<n;i++) 9 { 10 gets(p[i]); 11 for(int j=0;j<m;j++) 12 flag[i][j]=0; 13 } 14 cout<<"输入入口的坐标"<<endl; 15 cin>>k0.x>>k0.y; 16 cout<<"输入出口的坐标"<<endl; 17 cin>>jie.x>>jie.y; 18 system("cls"); 19 cout<<endl<<endl<<endl<<endl; 20 cout<<" 迷宫大小为"<<n<<"行"<<m<<"列"<<endl<<endl; 21 cout<<" 此次迷宫为:"<<endl; 22 for(int i=0;i<n;i++) 23 {cout<<" ";puts(p[i]);} 24 cout<<" 出发点为"<<"("<<k0.x<<","<<k0.y<<")"; 25 cout<<" 终点为"<<"("<<jie.x<<","<<jie.y<<")"<<endl; 26 cout<<endl<<endl<<endl; 27 }
jiegou.cpp
1 char p[100][100]; 2 int flag[100][100]; 3 int n,m; 4 int dx[]={-1,1,0,0}; 5 int dy[]={0,0,-1,1}; 6 struct pot 7 { 8 int x,y; 9 }; 10 pot k0,jie;
start.cpp
1 int dfs(pot t) 2 { 3 if(t.x<0||t.x>=n||t.y<0||t.y>=m||flag[t.x][t.y]==1||p[t.x][t.y]==‘0‘) 4 return 0; 5 return 1; 6 } 7 void lu() 8 { 9 stack<pot>s; 10 s.push(k0); 11 int t; 12 while(!s.empty()) 13 { 14 flag[k0.x][k0.y]=1; 15 if(k0.x==jie.x&&k0.y==jie.y) 16 {break;} 17 t=1; 18 pot k; 19 k=k0; 20 for(int i=0;i<4;i++) 21 { k0.x=k.x+dx[i]; 22 k0.y=k.y+dy[i]; 23 if(dfs(k0)) 24 {s.push(k0);t=0;break;} 25 } 26 if(t==0) 27 continue; 28 else 29 { 30 s.pop(); 31 k0=s.top(); 32 } 33 } 34 stack<pot>s0; 35 while(!s.empty()) 36 { k0=s.top(); 37 s0.push(k0); 38 s.pop(); 39 } 40 while(!s0.empty()) 41 { k0=s0.top(); 42 p[k0.x][k0.y]=‘*‘; 43 cout<<" "<<k0.x<<" "<<k0.y<<endl; 44 s0.pop(); 45 } 46 cout<<endl<<endl; 47 } 48 void tu() 49 { 50 for(int i=0;i<n;i++) 51 { cout<<" "; 52 for(int j=0;j<m;j++) 53 { 54 cout<<p[i][j]<<" "; 55 Sleep(N); 56 } 57 cout<<endl; 58 } 59 cout<<endl<<endl<<endl<<endl; 60 }
show.cpp
1 using namespace std; 2 void see2() 3 { cout<<endl<<endl; 4 printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4); 5 printf("%c %c\n",4,4); 6 printf("%c 1、输入迷宫 2、输出路径 %c\n",4,4); 7 printf("%c %c\n",4,4); 8 printf("%c 3、来张地图 4、运行结束 %c\n",4,4); 9 printf("%c %c\n",4,4); 10 printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4); 11 cout<<endl; 12 cout<<"-----------恭候您的选择,请直接用键盘输入选项------------"<<endl; 13 cout<<"您的选择是:"; 14 } 15 void see1() 16 { 17 cout<<endl<<endl<<endl; 18 printf(" ----------------迷宫大冒险------------------\n"); 19 printf(" %c %c\n",3,3); 20 printf(" %c * * * * * * * * * * * * * * * * * * * %c\n",3,3); 21 printf(" %c * %c * * * * * * * %c\n",3,2,3); 22 printf(" %c * * * * * * %c\n",3,3); 23 printf(" %c * * * * * * * * * * %c\n",3,3); 24 printf(" %c * * * * * * * * * %c\n",3,3); 25 printf(" %c * * * * * * * * %c\n",3,3); 26 printf(" %c * * * * * * * * %c\n",3,3); 27 printf(" %c * * * * * * * * * * * * * * %c\n",3,3); 28 printf(" %c * * * * * * * %c\n",3,3); 29 printf(" %c * * * * * * * * * * * * * * * * * * * %c\n",3,3); 30 printf(" %c %c\n",3,3); 31 printf(" --------------------------------------------\n"); 32 cout<<endl; 33 cout<<" -------开始冒险吧!--------"<<endl; 34 cout<<endl<<endl<<endl<<endl<<endl; 35 cout<<"--按任意键继续--"<<endl; 36 getchar(); 37 system("cls"); 38 }
时间: 2024-10-08 20:18:09