走迷宫,递归

来源:互联网 发布:linux装java环境 编辑:程序博客网 时间:2024/05/21 19:31
#include <stdio.h>#define MazeR 5#define MazeC 9int mark[MazeR+2][MazeC+2];struct offsetNode {    int up;    int left;    char *dir;} move[4];int seekPath(int Maze[MazeR+2][MazeC+2], int s_x, int s_y, int e_x ,int e_y);int main(void){    int Maze[MazeR+2][MazeC+2]={    {1,1,1,1,1,1,1,1,1,1,1},    {1,0,1,0,0,0,0,0,0,0,1},    {1,0,1,0,0,1,1,1,1,0,1},    {1,0,1,1,0,1,1,0,0,0,1},    {1,0,1,0,0,1,1,0,1,1,1},    {1,0,0,0,1,0,1,0,0,0,1},    {1,1,1,1,1,1,1,1,1,1,1}    };    int i,j;    move[0].up=1;move[0].left=0;move[0].dir="Down";    move[1].up=0;move[1].left=1;move[1].dir="Right";    move[2].up=-1;move[2].left=0;move[2].dir="Up";    move[3].up=0;move[3].left=-1;move[3].dir="Left";    for(i=0;i<MazeR+2;i++)        for(j=0;j<MazeC+2;j++){            mark[i][j]=0;            }    mark[1][1]=1;    seekPath(Maze,1,1,5,9);    return 0;    }int seekPath(int Maze[MazeR+2][MazeC+2], int s_x, int s_y, int e_x ,int e_y){    int i,x,y;    char *d=NULL;    if(s_x==e_x && s_y==e_y)        return 1;    for(i=0;i<4;i++){        x=s_x+move[i].up;        y=s_y+move[i].left;        d=move[i].dir;        if((Maze[x][y]==0) && (mark[x][y]==0))        {   mark[x][y]=1;            if(seekPath(Maze,x,y,e_x,e_y))            {   printf("(%d,%d),%s\n",x,y,d);                return 1;            }        }    }    if(s_x==1 && s_y==1)        printf("No avaible Path\n");    return 0;}
原创粉丝点击