## 关于C++的迷宫问题

```#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int maze[7][7]={{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2}};
int startx=1;
int starty=1;
int endx=5,endy=5;
void visit(int x,int y)//这个函数能求出所有的路径吗？
{
maze[x][y]=1;
if(x==endx,y==endy)
{

printf("The path of running out of the maze is given below:\n");
for(x=0;x<7;x++)
{
for(y=0;y<7;y++)
{
if(maze[x][y]==2)
printf("█");
else if(maze[x][y]==1)
printf("**");
else
printf("  ");
}
printf("\n");
}
}
if(maze[x-1][y]==0)
{
visit(x-1,y);
}
if(maze[x][y-1]==0)
{
visit(x,y-1);
}
if(maze[x+1][y]==0)
{
visit(x+1,y);
}
if(maze[x][y+1]==0)
{
visit(x,y+1);
}

maze[x][y]=0;//请问这句的作用是什么？为什么要把他的值从1赋为0？

}

int main()
{
int x,y;
printf("The map of the mzae is printed below :\n");
for(x=0;x<7;x++)
{
for(y=0;y<7;y++)
{
if(maze[x][y]==2)
printf("█");
else
printf("  ");
}
printf("\n");
}
visit(startx,starty);

return 0;
}

0

maze[x][y]=0 是因为先认为这条路是通的 所以设为1，假如最后不通 则标志为0

0

#### 引用来自“浪客Dandy”的答案

maze[x][y]=0 是因为先认为这条路是通的 所以设为1，假如最后不通 则标志为0

0不是表示这条路是通的吗？
0

```void Visit(int x, int y)
{
if (Maze[x][y] != FLAG_FREE)
return; //Exit recursion

//oh.. It is a visitable position.

Maze[x][y] = FLAG_FREE_BUSY; //Visiting

if(x == EndX && y == EndY)
{
printf("End\n");
for(x=0;x<7;x++)
{
for(y=0;y<7;y++)
{
if(Maze[x][y] == FLAG_FIRM)
printf("█");
else if(Maze[x][y] == FLAG_FREE_BUSY)
printf("**");
else
printf("  ");
}
printf("\n");
}
}

Visit(x-1, y); //Try visiting left
Visit(x, y-1); //Try visiting up
Visit(x+1, y); //Try visiting right
Visit(x, y+1); //Try visiting down

Maze[x][y] = FLAG_FREE; //Visited
}```

#define FLAG_FREE            0

#define FLAG_FREE_BUSY    1

#define FLAG_FIRM            2