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;
}