走迷宫问题

一个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
",findpath);
36     printf("%d
",count);
37     system("pause");
38 }
大多数想法要么平庸,要么更糟糕,这很大程度上因为绝妙的想法难得一见,而且他们还要在我们身边这个充斥了各种恶俗的所谓常识的环境中孕育生长。
原文地址:https://www.cnblogs.com/linux0537/p/6070325.html