用递归思想解迷宫问题

来源:互联网 发布:淘宝禁售规则 编辑:程序博客网 时间:2024/04/29 04:45
#include <stdio.h>

#define MAX_ROW    8
#define MAX_COL    8

int flag = 0;

typedef struct point
{
    int row;
    int col;
}Point;

int maze[MAX_ROW][MAX_COL] = {
    0, 1, 0, 1, 0, 0, 1, 0,
    0, 0, 1, 0, 1, 0, 0, 1,
    0, 0, 0, 0, 1, 1, 1, 1,
    1, 0, 1, 0, 1, 0, 0, 1,
    1, 0, 1, 0, 0, 1, 1, 1,
    1, 1, 1, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 1, 1, 1,
    1, 1, 1, 1, 0, 0, 0, 0,
};

Point FootPrints[MAX_ROW][MAX_COL] = {
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
};

void Print_Maze(void)
{
    int i = 0, j = 0;

    for(i = 0; i < MAX_ROW; i++)
    {
        for(j = 0; j < MAX_COL; j++)
        {
            printf("%2d", maze[i][j]);
        }
        
        printf("\n");
    }

    printf("\n*****************************************  \n");
}

void Print_Foots(void)
{
    Point p = {7, 7};
    while(FootPrints[p.row][p.col].row != -1)/*FootPrints[p.row][p.col]记录p点的上一个点的坐标*/
    {
        printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
        p = FootPrints[p.row][p.col];
        printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
    }

    printf("seccess out ! \n");
}

void Print(void)
{
    int i = 0, j = 0;

    for(i = 0; i < MAX_ROW; i++)
    {
        for(j = 0; j < MAX_COL; j++)
        {
            printf("{%d, %d},", FootPrints[i][j].row, FootPrints[i][j].col);
        }

        printf("\n");
    }

    printf("\n");
}

void VisitPoint(Point p)
{
    Print_Maze();/*打印迷宫*/

    if((p.row+1 == MAX_ROW) && (p.col+1 == MAX_COL))
    {
        printf("it's go out ! \n");
        flag = 1;

        return;
    }

    if((p.row+1 < MAX_ROW) && (maze[p.row+1][p.col] == 0))
    {
        Point tempPoint = {p.row+1, p.col};

        maze[p.row+1][p.col] = 2;
        FootPrints[p.row+1][p.col] = p;
        VisitPoint(tempPoint);
    }

    if((p.col+1 < MAX_COL) && (maze[p.row][p.col+1] == 0))
    {
        Point tempPoint = {p.row, p.col+1};

        maze[p.row][p.col+1] = 2;
        FootPrints[p.row][p.col+1] = p;
        VisitPoint(tempPoint);
    }

    if((p.row-1 >= 0) && (maze[p.row-1][p.col] == 0))
    {
        Point tempPoint = {p.row-1, p.col};

        maze[p.row-1][p.col] = 2;
        FootPrints[p.row-1][p.col] = p;
        VisitPoint(tempPoint);
    }

    if((p.col-1 >= 0) && (maze[p.row][p.col-1] == 0))
    {
        Point tempPoint = {p.row, p.col-1};

        maze[p.row][p.col-1] = 2;
        FootPrints[p.row][p.col-1] = p;
        VisitPoint(tempPoint);
    }
    
    return;
}

int main(void)
{
    Point p = {0, 0};/*出发点*/

    maze[p.row][p.col] = 2;/*访问过的点标记为2*/

    VisitPoint(p);
    if(flag)
    {
        Print_Foots();/*打印成功走出迷宫的路径*/
    }
    else
    {
        printf("No Path ! \n");
    }

    return    0;
}
0 0
原创粉丝点击