逃离迷宫--输入创造迷宫

原本看到有道题是做在5*5下的迷宫找出最优路径,心血来潮做了个任意起点、终点,任意地图大小(只需在宏定义修改)的迷宫

  1 #include <stdio.h>
  2 #include <math.h>
  3 
  4 #define MAX_lENTH 9999 //最长步数, 
  5 #define MAP_SIDE_LENTH 16 //地图边长大小 
  6 #define MAP_START_POINT   (MAP_SIDE_LENTH+1) //地图起始点 
  7 #define CLEAR_POINT 0//可行走的点 
  8 #define DISORDER_POINT 3//障碍点 
  9 #define OUTSIDE_POINT 4//迷宫外的点 
 10  
 11 int map[MAP_SIDE_LENTH*MAP_SIDE_LENTH];//地图
 12 int move[]={MAP_SIDE_LENTH,1,-1,-MAP_SIDE_LENTH};//走法 
 13 int currentPathLenth=0;//当前走的路径长度 
 14 int bestPathLenth=MAX_lENTH;//最好的路径 
 15 int movePath[1000];//行走轨迹 
 16 int currentMovePath[100];//当前行走轨迹 
 17 int movePathIndex=0;//行走轨迹下标 
 18 int mazeSideLenth;//迷宫的边长 
 19 int mazeEntrance;//迷宫入口 
 20 int mazeExit;//迷宫出口
 21 
 22 //我的输入 
 23 /*
 24 1000000
 25 3333330
 26 0000030
 27 0333030
 28 0332030
 29 0333330
 30 0000000
 31 */
 32 
 33 void findBestPath(int depth)
 34 {
 35     if(depth==mazeExit)
 36     {
 37         if(currentPathLenth<bestPathLenth)//迷宫最后一点 
 38         {
 39             bestPathLenth=currentPathLenth;    
 40             for(int i=0;i<bestPathLenth;i++)
 41                 movePath[i]=currentMovePath[i];
 42             movePath[bestPathLenth]=depth;
 43         }
 44         
 45         return ;
 46     }    
 47     
 48     for(int i=0;i<4;i++)
 49     {
 50         if(!map[depth+move[i]])    
 51         {
 52             currentPathLenth++;
 53             map[depth]=DISORDER_POINT;
 54             currentMovePath[movePathIndex++]=depth;
 55             
 56             findBestPath(depth+move[i]);
 57             
 58             movePathIndex--;
 59             currentPathLenth--;
 60             map[depth]=CLEAR_POINT;
 61         }
 62     }
 63     
 64 }
 65 //用户自定义迷宫 
 66 int userDefined() 
 67 {
 68     printf("输入迷宫,0,表示可以行走,1表示迷宫入口,2表示迷宫出口,3表示该位置被挡住\n");
 69     int overTimes=0;//两次回车结束自定义迷宫 
 70     for(int i=0;i<MAP_SIDE_LENTH;i++)
 71     {
 72         if(overTimes>=2)
 73         {
 74             for(int j=i+1;j<MAP_SIDE_LENTH;j++)
 75                 for(int k=0;k<MAP_SIDE_LENTH;k++)
 76                         map[(j*MAP_SIDE_LENTH)+k]=OUTSIDE_POINT;
 77             break;
 78         }
 79         for(int j=0;j<MAP_SIDE_LENTH;j++)
 80         {
 81             if(i==0 || i==MAP_SIDE_LENTH-1)    
 82             {
 83                 for(int k=0;k<MAP_SIDE_LENTH;k++)    
 84                     map[(i*MAP_SIDE_LENTH)+k]=DISORDER_POINT;
 85                 break;
 86             } 
 87             if(j==0 || j==MAP_SIDE_LENTH-1) 
 88             {
 89                 map[(i*MAP_SIDE_LENTH)+j]=DISORDER_POINT;
 90                 continue;
 91             }
 92             
 93             char temp=getchar();
 94             if(j>=MAP_SIDE_LENTH-1 && temp!='\n')
 95             {
 96                 printf("超出世界的边境!");
 97             
 98                 return 1; 
 99             }
100             if(temp=='\n')
101             {
102                 overTimes++;
103                 
104                 if(overTimes==1)
105                 {
106                     map[i*MAP_SIDE_LENTH+j]=DISORDER_POINT;
107                     for(int k=j+1;k<MAP_SIDE_LENTH;k++)    
108                         map[i*MAP_SIDE_LENTH+k]=OUTSIDE_POINT;
109                 }
110                 else
111                 {
112                     for(int k=0;k<MAP_SIDE_LENTH;k++)
113                         map[i*MAP_SIDE_LENTH+k]=DISORDER_POINT;
114                 }
115                     
116                 break;
117             }
118             overTimes=0;
119             if(temp=='1')    mazeEntrance=i*MAP_SIDE_LENTH+j;
120             else if(temp=='2')    mazeExit=i*MAP_SIDE_LENTH+j;
121             else     map[(i*MAP_SIDE_LENTH)+j]=temp-'0';
122         }
123     }
124     
125     return 0;
126 }
127 
128 
129 
130 //初始化地图 
131 int  initialization()
132 {
133     
134     return userDefined();
135 } 
136 
137 int main()
138 {
139     initialization(); 
140     
141     printf("显示迷宫\n");
142     for(int i=0;i<MAP_SIDE_LENTH;i++)
143     {
144         for(int j=0;j<MAP_SIDE_LENTH;j++)
145             if(!map[i*MAP_SIDE_LENTH+j])    printf("  ");
146             else if(map[i*MAP_SIDE_LENTH+j]==3)printf("[]");
147         printf("\n");
148     }
149     
150     findBestPath(mazeEntrance);
151 
152     if(bestPathLenth==MAX_lENTH)    
153     {
154         //表示死路一条 
155         printf("这是死胡同啊!");
156         
157         return 1;
158     }
159     printf("行走轨迹(相对于迷宫)\n");
160     for(int i=0;i<=bestPathLenth;i++)
161     {
162         printf("(%d,%d) ",movePath[i]/MAP_SIDE_LENTH-1,movePath[i]%MAP_SIDE_LENTH-1);
163     }
164     return 0;
165 }
View Code

 

posted @ 2014-03-09 10:39  随心随想  阅读(421)  评论(0编辑  收藏  举报