64,643
社区成员
发帖
与我相关
我的任务
分享
#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);
}
}
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);
}
}