迷宫问题

1.对于迷宫问题很好奇,想用递归的方式解决,但是不知道思路是什么,一直对于递归没有什么想法,想用最简洁的方法解决迷宫找路的问题,本来写了一个算法,是比较简单,但是在一些情况下会出错,但是看网上很多人写的太复杂了,没太大的参考的价值

 

现在在网上看到了一个算法,希望能成功

// maze.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <stdio.h>

 

int zmap[6][6]=

{

1,1,1,1,1,1,

1,0,1,1,0,1,

1,0,1,0,0,1,

1,0,0,0,1,1,

1,1,1,0,0,1,

1,1,1,1,1,1

 

};

 

#define MAX_LEN   6*6

 

struct NODE{

 

    int row;           /* the row of the position */

 

    int col;           /* the col of the position */

 

} path[MAX_LEN];

 

 

struct mazmove

{

 

     int row,col;

 

}offsets[4]={           {0,1 },  /* move to east */

 

                       {1,0 }, /* move to south */

 

                       {0,-1}, /* move to west */

 

                       {-1,0}  /* move to north */

 

                       };

int len=0;

 

int zfindpath(int row,int col,int erow,int ecol)

 

{

    int dir,row2,col2;

    if((row==erow)&&(col==ecol))

    { 

path[0].row=erow;

   path[0].col=ecol;    /* judge if is the exit出口 */

     return 1;

}

zmap[row][col]=2;             /* block current position */

for(dir=0;dir<=4;dir++)

{

row2=row+offsets[dir].row;  /* step to next space */

col2=col+offsets[dir].col;  /* if want to observe the trace of the ball,you should add code after here!*/

if(zmap[row2][col2]==0)      /* mean the way could go at this stage */

len=zfindpath(row2,col2,erow,ecol);

if(len>0)

{

path[len].row=row2;      /* use recursion to find the path  */

path[len].col=col2;

return ++len;

}

}

  }

return 0;

}

 

void show()

{

int i,j;

for (i = 0 ; i < 6 ; i++) {

for (j = 0 ; j < 6 ; j++) {

printf("  %d  ",zmap[i][j]);

}

printf("/n");

}

}

 

int main(int argc, char* argv[])

{

zfindpath(1,1,4,4);

show();

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值