迷宫递归解法求助

stackoverlow 2011-07-21 03:22:26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_ROWS 5
#define NUM_COLS 3
#define BOUNDARY_COLS 5
#define MAX_STACK_SIZE 100
#define FALSE 0
#define TRUE 1

typedef struct {
short int row;
short int col;
short int dir;
} element;

element stack[MAX_STACK_SIZE]; /* global stack declaration */


typedef struct {
short int vert;
short int horiz;
} offsets;


offsets move[9]; /* array of moves for each direction */


static short int maze[][BOUNDARY_COLS] = {{1,1,1,1,1}, /* top boundary */
{1,0,0,0,1},
{1,1,1,0,1},
{1,0,0,0,1},
{1,0,1,1,1},
{1,0,0,0,1},
{1,1,1,1,1}}; /* bottom boundary */

short int mark[][BOUNDARY_COLS] = {{0,0,0,0,0},
{0,1,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}};

int top=0;
int found = FALSE;
void init_move();
void add(element);
element delete();
void stack_full();
void stack_empty();
void path();
void print_record(int,int,int);
void print_maze();



int main ()
{/* stack represented as an array */
init_move();
print_maze();
path(1,1);
system("pause");
return 0;
}


void init_move()
{/* initial the table for the next row and column moves */
move[1].vert = -1; move[1].horiz = 0; /* N */
move[2].vert = -1; move[2].horiz = 1; /* NE */
move[3].vert = 0; move[3].horiz = 1; /* E */
move[4].vert = 1; move[4].horiz = 1; /* SE */
move[5].vert = 1; move[5].horiz = 1; /* S */
move[6].vert = 1; move[6].horiz = 0; /* SW */
move[7].vert = 0; move[7].horiz = -1; /* W */
move[8].vert = -1; move[8].horiz = -1; /* NW */
}


void print_maze()
{/* print out the maze */
int i,j;
printf("Your maze, with the boundaries is: \n\n");
for (i = 0; i <= NUM_ROWS+1; i++) {
for(j = 0; j <= NUM_COLS+1; j++)
printf("%3d",maze[i][j]);
printf("\n");
}
printf("\n");
}


void stack_full()
{
printf("The stack is full. No item added \n");
}


void stack_empty()
{
printf("The stack is empty. No item deleted \n");
}



void add(element item)
{ /* add an item to the global stack
top (also global) is the current top of the stack,
MAX_STACK_SIZE is the maximum size */

if (top == MAX_STACK_SIZE)
stack_full();
else
stack[++top] = item;
}



element delete()
{ /* remove top element from the stack and put it in item */
if (top < 0)
{
stack_empty();
exit(EXIT_FAILURE);
system("pause");
}
else
return stack[top--];
}


void print_record(int row, int col, int dir)
{ /* print out the row, column, and the direction, the direction
is printed out with its numeric equivvalent */
printf("%2d %2d%5d", dir,row, col);
switch (dir) {
case 1: printf(" N");
break;
case 2: printf(" NE");
break;
case 3: printf(" E ");
break;
case 4: printf(" SE");
break;
case 5: printf(" S ");
break;
case 6: printf(" SW");
break;
case 7: printf(" W ");
break;
case 8: printf(" NW");
break;
}
printf("\n");
}


element position;
void path(int row,int col)
{
static int dir=0;
int i, next_row, next_col;
if(dir=0)
{
position.col=col;
position.row=row;
position.dir=2;
}
// row = position.row; col = position.col; dir = position.dir;
while(top>-1&&!found)
{
if(dir>8)
{
position = delete();
row = position.row; col = position.col; dir = position.dir;
path(row,col);
}
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if (next_row == NUM_ROWS && next_col == NUM_COLS)
{
found = TRUE;
break;
}
else
if ( !maze[next_row][next_col]&& !mark[next_row][next_col])
{
mark[next_row][next_col]=TRUE;
position.row=row;position.col=col;position.dir=dir;
add(position);
dir=1;
path(next_row,next_col);
}
else
{
++dir;
}
}

if (!found)
printf("The maze does not have a path\n");
else {
/* print out the path which is found in the stack */
printf("The maze traversal is: \n\n");
printf("dir# row col dir\n\n");
for (i = 0; i <= top; i++)
print_record(stack[i].row, stack[i].col, stack[i].dir);
printf(" %2d%5d\n",row,col);
printf(" %2d%5d\n",NUM_ROWS,NUM_COLS);
}
}

上面是全部代码,我把循环解法改成递归的了,结果第一个结果是想要的结果,但后面还连着输出了好多个结果而且不是正确的,循环版本就只有一个结果。。。求各位大神帮看看path函数逻辑上有什么问题
...全文
128 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyuwww 2011-07-21
  • 打赏
  • 举报
回复
CSDN上面很少有人帮你该程序的 你还不如到百度上面 但是你的程序这么长100分估计才能拿下。。我是真的没时间也未必有能力 我自己写的迷宫也仍在哪里 我在CSDN上发的改程序的东西基本没人踩 要不就是那些人随便了事的随便讲 很少有人耐心看你的代码错在哪里
stackoverlow 2011-07-21
  • 打赏
  • 举报
回复
void path(int row,int col)
{
static int dir=0;
int i, next_row, next_col;
if(dir=0)
{
position.col=col;
position.row=row;
position.dir=2;
}
// row = position.row; col = position.col; dir = position.dir;
while(top>-1&&!found)
{
if(dir>8)
{
position = delete();
row = position.row; col = position.col; dir = position.dir;
path(row,col);
}
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if (next_row == NUM_ROWS && next_col == NUM_COLS)
{
found = TRUE;
break;
}
else
if ( !maze[next_row][next_col]&& !mark[next_row][next_col])
{
mark[next_row][next_col]=TRUE;
position.row=row;position.col=col;position.dir=dir;
add(position);
dir=1;
path(next_row,next_col);
}
else
{
++dir;
}
}

if (!found)
printf("The maze does not have a path\n");
else {
/* print out the path which is found in the stack */
printf("The maze traversal is: \n\n");
printf("dir# row col dir\n\n");
for (i = 0; i <= top; i++)
print_record(stack[i].row, stack[i].col, stack[i].dir);
printf(" %2d%5d\n",row,col);
printf(" %2d%5d\n",NUM_ROWS,NUM_COLS);
}
}

求助啊没人理我么55555
嫌长帮我看看这个函数逻辑对没应该就行吧。。求解- -
wuyuwww 2011-07-21
  • 打赏
  • 举报
回复
hao niu 啊 我的迷宫没搞出来就一直放在那里

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧