您的位置:首页 > 其它

迷宫问题的递归求解

2011-03-11 23:46 447 查看
///dabbysunshine@qq.com

///来源:《C语言程序设计(C99)》程良银.游洪跃等P266-269
///迷宫问题的递归求解.
///=============================================================================
#include <stdio.h>
#include <stdlib.h>

#define W 80  //迷宫宽的最大值
#define H 60  //迷宫高的最大值

/*
迷宫单元格状态:
VIA:已经过;BLOCK:阻塞;EMPTY:空的.
*/
typedef enum {VIA,BLOCK,EMPTY} MazeCellStatus;

typedef struct
{
int x,y;  //单元格的坐标
} CellType;  //迷宫单元格的类型

typedef struct
{
CellType path[W*H];   //经过路径
int         length;   //经过长度
} MazePathType;           //迷宫路线类型

///=============================================================================

void outSolution(MazePathType mazePath);             //输出迷宫问题的解
void trySolution(MazeCellStatus maze[][W],int w,int h,CellType exit,
CellType cur,MazePathType mazePath);//试探求解下一位置
void mazeSolution(MazeCellStatus maze[][W],int w,int h,
CellType entry,CellType exit);     //迷宫问题

///=============================================================================
/**主函数部分**/
int main(void)
{
MazeCellStatus maze[H][W] =
{
{EMPTY,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK},
{EMPTY,BLOCK,EMPTY,EMPTY,BLOCK,EMPTY,EMPTY,EMPTY,BLOCK,BLOCK},
{EMPTY,EMPTY,EMPTY,BLOCK,BLOCK,EMPTY,BLOCK,EMPTY,EMPTY,BLOCK},
{EMPTY,BLOCK,BLOCK,EMPTY,EMPTY,EMPTY,BLOCK,BLOCK,EMPTY,BLOCK},
{EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,BLOCK},
{BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,BLOCK,EMPTY,EMPTY},
};

CellType entry = {0,0};  /*入口位置*/
CellType exit  = {9,5};   /*出口位置*/
int h = 6,w = 10;       /*迷宫的高与宽*/

mazeSolution(maze,w,h,entry,exit);  ///求解迷宫

return 0;
}

///=============================================================================
void mazeSolution(MazeCellStatus maze[][W],int w,int h,
CellType entry,CellType exit)
{
MazePathType mazePath;
mazePath.length = 0;   /*迷宫路线初始长度为0*/

/*将入口存入路径,作为路径的起始点*/
mazePath.path[mazePath.length].x = entry.x;
mazePath.path[mazePath.length].y = entry.y;
mazePath.length ++;            //路径长自增1

trySolution(maze,w,h,exit,entry,mazePath); ///用递归求解迷宫问题

}

void trySolution(MazeCellStatus maze[][W],int w,int h,CellType exit,
CellType cur,MazePathType mazePath)
{
int i;                    ///试探求解下一位置
int xShift[4] = {0,0,-1,1};  //相邻位置相对于当前位置的x坐标
int yShift[4] = {-1,1,0,0};  //相邻位置相对于当前位置的y坐标
CellType adjCell;            //当前位置的相邻位置

if(cur.x == exit.x && cur.y == exit.y)
{
//已达到出口,输出解
outSolution(mazePath);
}
else
{
for(i = 0; i < 4; i ++)
{
adjCell.x = cur.x + xShift[i];   //求相邻位置的x坐标
adjCell.y = cur.y + yShift[i];   //求相邻位置的y坐标

if(adjCell.x >= 0 && adjCell.y >= 0 && adjCell.x < w
&& adjCell.y < h && (maze[adjCell.y][adjCell.x] == EMPTY))
{
//相邻位置在迷宫内,并且为空白
//将相邻位置存入路径中
mazePath.path[mazePath.length].x = adjCell.x;
mazePath.path[mazePath.length].y = adjCell.y;
mazePath.length ++;       //路径长自增1

maze[adjCell.y][adjCell.x] = VIA;   //经过相邻位置

///对相邻位置进行递归
trySolution(maze,w,h,exit,adjCell,mazePath);

/*从路径中去掉adjCell,路径长度将自减1*/
mazePath.length --;
/*恢复相邻位置为空白,回溯*/
maze[adjCell.y][adjCell.x] = EMPTY;
}
}
}
}

void outSolution(MazePathType mazePath)
{
//输出迷宫的解
static int num = 0;
int i;

printf("第%d条路径:/n",++ num);  //num为当前以求得解得个数

for(i = 0; i < mazePath.length; i ++)
{
printf("(%d,%d)",mazePath.path[i].x,mazePath.path[i].y);
if((i + 1)%5 == 0)
{
printf("/n");
}
}
printf("/n/n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: