迷宫求解设计——数据结构的课程设计 10
要求:可以输入任意大小的迷宫数据用非递归方法求一条走出迷宫的路径必须是用非递归求解的输出路径必须是从起点到终点...
要求:可以输入任意大小的迷宫数据
用非递归方法求一条走出迷宫的路径
必须是用非递归求解的
输出路径必须是从起点到终点 展开
用非递归方法求一条走出迷宫的路径
必须是用非递归求解的
输出路径必须是从起点到终点 展开
2个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
#define N 10
#define M 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int findway(int);
int NextStep(int *, int *, int );
typedef struct
{
int x, y;
int dir;
}ElemType;
typedef struct StackNode
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,ElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if (!S->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S->top = S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++=e;
return OK;
}
int Pop(SqStack *S,ElemType *e)
{
if(S->top==S->base)
{
return ERROR;
}
*e=*S->top;
return OK;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
void Input(char b[M][N])
{
int i, j;
printf("qing shu ri migong xing zhuang:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
scanf("%c",&b[i][j]);
}
getchar();
}
}
void Ouput(const char b[M][N])
{
int i, j;
printf("migong de xing zhuang wei:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
printf("%c",b[i][j]);
}
printf("\n");
}
}
int FindWay(char maze[M][N])
{
ElemType e;
int constep = 1;
int x = 1, y = 1;
SqStack S;
InitStack(&S);
do
{
if (maze[x][y] == ' ')
{
maze[x][y] = '1';
e.x = x;
e.y = y;
e.dir = 1;
Push(&S,e);
if (x == M-1 && y == N-1)
{
printf("chu zai chu kou\n");
return 1;
}
NextStep(&x,&y,1);
constep++;
}
else
{
Pop(&S,&e);
while (e.dir == 4 && !StackEmpty(&S))
{
maze[e.x][e.y] ='0';
Pop(&S,&e);
}
{
if (e.dir < 4)
{
e.dir++;
Push(&S,e);
x = e.x;
y = e.y;
NextStep(&x, &y, e.dir);
}
else
{
printf("mei you chu kou\n");
return 0;
}
}
}
}while(S.top!=S.base);
return 0;
}
int NextStep(int *x, int *y, int dir)
{
switch(dir)
{
case 1:
(*y)++;
break;
case 2:
(*x)++;
break;
case 3:
(*y)--;
break;
case 4:
(*x)--;
break;
default:
break;
}
return 0;
}
int main(void)
{
char a[M][N];
Input(a);
Ouput(a);
FindWay(a);
Ouput(a);
system("pause");
return 0;
}
#include<stdlib.h>
#define N 10
#define M 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int findway(int);
int NextStep(int *, int *, int );
typedef struct
{
int x, y;
int dir;
}ElemType;
typedef struct StackNode
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,ElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if (!S->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
S->top = S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++=e;
return OK;
}
int Pop(SqStack *S,ElemType *e)
{
if(S->top==S->base)
{
return ERROR;
}
*e=*S->top;
return OK;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
void Input(char b[M][N])
{
int i, j;
printf("qing shu ri migong xing zhuang:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
scanf("%c",&b[i][j]);
}
getchar();
}
}
void Ouput(const char b[M][N])
{
int i, j;
printf("migong de xing zhuang wei:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
printf("%c",b[i][j]);
}
printf("\n");
}
}
int FindWay(char maze[M][N])
{
ElemType e;
int constep = 1;
int x = 1, y = 1;
SqStack S;
InitStack(&S);
do
{
if (maze[x][y] == ' ')
{
maze[x][y] = '1';
e.x = x;
e.y = y;
e.dir = 1;
Push(&S,e);
if (x == M-1 && y == N-1)
{
printf("chu zai chu kou\n");
return 1;
}
NextStep(&x,&y,1);
constep++;
}
else
{
Pop(&S,&e);
while (e.dir == 4 && !StackEmpty(&S))
{
maze[e.x][e.y] ='0';
Pop(&S,&e);
}
{
if (e.dir < 4)
{
e.dir++;
Push(&S,e);
x = e.x;
y = e.y;
NextStep(&x, &y, e.dir);
}
else
{
printf("mei you chu kou\n");
return 0;
}
}
}
}while(S.top!=S.base);
return 0;
}
int NextStep(int *x, int *y, int dir)
{
switch(dir)
{
case 1:
(*y)++;
break;
case 2:
(*x)++;
break;
case 3:
(*y)--;
break;
case 4:
(*x)--;
break;
default:
break;
}
return 0;
}
int main(void)
{
char a[M][N];
Input(a);
Ouput(a);
FindWay(a);
Ouput(a);
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询