Algorithms_Maze

#include"STDIO.H"
#define N 7
#define FLAG 2

int maze[N][N]={{1,1,1,1,1,1,1},
				{1,0,1,0,1,0,1},
				{1,0,0,0,0,1,1},
				{1,0,1,0,0,0,1},
				{1,0,1,0,1,0,1},
				{1,0,1,0,0,0,1},
				{1,1,1,1,1,1,1}};
int startx=1;
int starty=1;
int endx=5;
int endy=5;
int success=0;

int fine(int x,int y,int map[N][N]);
void main()
{
	if(fine(startx,starty,maze))
		printf("Success!\n");
	else
		printf("Fail!\n");
	for (int i=0;i < N;i++)
	{
		for(int j=0;j < N;j++)
			if(maze[i][j]==1)
				printf("■");
			else if(maze[i][j]==2)
				printf("◇");
			else
				printf("  ");
		printf("\n");
	}
}

int fine(int x,int y,int map[N][N])
{
	map[x][y]=FLAG;
	if(x==endx&&y==endy)
		success=1;
	if (!success&&map[x][y+1]==0)fine(x,y+1,map);
	if (!success&&map[x+1][y]==0)fine(x+1,y,map);
	if (!success&&map[x-1][y]==0)fine(x-1,y,map);
	if (!success&&map[x][y-1]==0)fine(x,y-1,map);

	if(!success)
		map[x][y]=0;//若此路未通,则清空标记
	return success;
}

你可能感兴趣的:(algorithms)