简单迷宫求解

迷宫求解十个很大众的问题了,而这个考点好多。其中涉及到了栈,递归,遍历,回溯等知识点的概念。

如图:

---------------main-------------
        迷宫地图                      成功走完迷宫的路线图
 0 1 0 0 0 0 0 0 0                   0 2 0 0 0 0 0 0 0
 0 1 0 0 0 0 0 0 0                   0 2 0 0 0 0 0 0 0
 0 1 1 1 1 0 0 0 0                   0 2 2 2 2 0 0 0 0
 1 1 0 0 1 1 1 1 1                   2 2 0 0 2 2 2 2 2
 0 0 0 0 1 0 0 0 0                   0 0 0 0 2 0 0 0 0
 0 0 0 0 1 0 0 0 0                   0 0 0 0 2 0 0 0 0
 0 0 0 0 1 0 0 0 0                   0 0 0 0 2 0 0 0 0 
 0 1 1 1 1 0 0 0 0                   0 2 2 2 2 0 0 0 0  
 0 0 0 0 1 0 0 0 0                   0 0 0 0 2 0 0 0 0  

备注:1是路,0是墙,2是走过的路

如图:
这里写图片描述

maze.c:

#include <stdio.h>
#include <stdlib.h>

#define ROW 9//定义行数
#define COL 9//定义列数
#define LINE printf("---------------%s-------------\n",__FUNCTION__);
//设计迷宫地图
int maze[ROW][COL] = {
    {0,1,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0},
    {0,1,1,1,1,0,0,0,0},
    {1,1,0,0,1,1,1,1,1},
    {0,0,0,0,1,0,0,0,0},
    {0,0,0,0,1,0,0,0,0},
    {0,0,0,0,1,0,0,0,0},
    {0,1,1,1,1,0,0,0,0},
    {0,0,0,0,1,0,0,0,0}
};
//定义坐标结构体,每次你走的坐标
typedef struct node{
    int row;
    int col;
}node;
//打印地图
void mazeprint(int maze[][COL],char* msg)
{
    printf("%s\n",msg);
    int i = 0;
    int j = 0;
    for(;i<ROW;i++)
    {
        for(j=0;j<COL;j++)
        {
            printf("%2d",maze[i][j]);
        }
        printf("\n");
    }
}
//判断这个点能不能走
int canstay(node new_node)
{
    if((new_node.col) < 0 || (new_node.row) < 0
            || (new_node.col) > COL|| (new_node.row) > ROW)
    {
          //在地图外
        return 0;
    }
    if(maze[new_node.row][new_node.col] == 0 || maze[new_node.row][new_node.col] == 2)
    {
        //遇到墙或者走过的路
        return 0;
    }
    if(maze[new_node.row][new_node.col] == 1)
    {
        return 1;
    }
}
//给走过的路做标记
void makenode(node new_node)
{
    maze[new_node.row][new_node.col] = 2;
}
//每次走下一步之前,判断当前点是不是出口
int isexit(node new_node,node pnode)
{
    if((new_node.col) != (pnode.col) && (new_node.row) != (pnode.row))
    {
        if(new_node.row == ROW-1 || new_node.col == COL-1
                || new_node.row == 0 || new_node.col == 0)
        {
            return 1;
        }
    }
    return 0;
}
//每次向四周遍历 按照顺时针方向
void ergodic(node new_node,node pnode)
{
    if(canstay(new_node) == 0)
    {
        return;
    }

    //printf("new_node:(%d,%d)\n",new_node.row,new_node.col);
    makenode(new_node);
    if(isexit(new_node,pnode) == 1)
    {
        printf("找到一条出路,出口是:");
        printf("(%d,%d)\n",new_node.row,new_node.col);
        return;
    }
    //借用系统的调用栈来实现递归
    node unode = {new_node.row-1,new_node.col};
    ergodic(unode,pnode);

    node rnode = {new_node.row,new_node.col+1};
    ergodic(rnode,pnode);

    node dnode = {new_node.row+1,new_node.col};
    ergodic(dnode,pnode);

    node lnode = {new_node.row,new_node.col-1};
    ergodic(lnode,pnode);
}

//开始走
void runstart(node pnode)
{
    ergodic(pnode,pnode);
}

int main()
{
    LINE;
    node pnode;
    pnode.row = 0;
    pnode.col = 1;
    mazeprint(maze,"   迷宫地图");
    runstart(pnode);
    mazeprint(maze,"  走过的路线");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/q496958148/article/details/80149134