首页 > 代码库 > 走迷宫问题

走迷宫问题

一个10*10的迷宫,每一个格子里面存0或者1,0代表可以走,1代表障碍。

输入值:起始坐标

输出值:能否达到设定的目标坐标点(Bool型变量,1或者0)

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define EX 2
 5 #define EY 2
 6 bool findpath = false;
 7 int DX[4]={-1,1,0,0};
 8 int DY[4]={0,0,-1,1};
 9 int maze[10][10]={{0,0,0,1,1,1,1,1,1,1},{1,0,1,1,1,1,1,1,1,1},{1,0,0,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{0,1,1,0,1,0,0,1,1},{0,1,1,0,1,0,0,1,1},{0,1,1,0,1,0,0,1,1},{0,1,1,0,1,0,0,1,1},{0,1,1,0,1,0,0,1,1},{0,0,0,0,0,0,0,1,1,1}};
10 int count;
11 
12 void DFS(int x,int y){
13     if(x==EX&&y==EY){
14       findpath = true;
15       count++;
16       return;
17     }
18     for(int i=0;i<4;i++){
19         int NX = x +DX[i];
20         int NY = y +DY[i];
21         if(NX<=10&&NY<=10&&NX>=0&&NY>=0&&maze[NX][NY]==0)
22         {
23             int tmp = maze[NX][NY];
24             maze[NX][NY] = 2;
25             DFS(NX,NY);
26             maze[NX][NY] = tmp; 
27         }
28 
29     }
30 }
31 
32 int main(){
33 
34     DFS(0,0);
35     printf("%d\n",findpath);
36     printf("%d\n",count);
37     system("pause");
38 }

 

走迷宫问题