#include <stdio.h>#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
#define STACKINCREMENT 10
#define STACK_INIT_SIZE 200
//typedef int SElemType;
typedef int Status;
typedef struct
{
int x;
int y;
}PosType;
typedef struct
{
int ord;
PosType seat;
int di;
}SElemType;
typedef struct
{
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
int map[20][20];
Status InitStack(SqStack *s)//建堆疊
{
s->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base)
return ERROR;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack *s,SElemType e)//入堆疊
{
if(s->top-s->base>=s->stacksize)
{
s->base=(SElemType*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s->base)
return ERROR;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
Status Pop(SqStack *s,SElemType *e)//出堆疊
{
if(s->top==s->base)
return ERROR;
*e=*--s->top;
return OK;
}
Status StackEmpty(SqStack s)//判空
{
if(s.top==s.base)
return TRUE;
else
return FALSE;
}
void Maze()
{
//int map[20][20];
int i,j,k;
srand(time(NULL));
map[1][0]=map[1][1]=map[18][19]=0;
for(j=0;j<20;j++)
map[0][j]=map[19][j]=1;
for(i=2;i<19;i++)
map[i][0]=map[i-1][19]=1;
for(i=1;i<19;i++)
{
for(j=1;j<19;j++)
{
k=rand()%3;
if(k)
map[i][j]=0;
else
map[i][j]=1;
}
//printf("\n");
}
}
PosType NextPos(PosType *e,int d)
{
PosType q;
switch(d)
{
case 1:q.x=e->x;
q.y=e->y+1;
break;
case 2:q.x=e->x+1;
q.y=e->y;
break;
case 3:q.x=e->x;
q.y=e->y-1;
break;
case 4:q.x=e->x-1;
q.y=e->y;
break;
}
return q;
}
Status Pass(PosType e)
{
//int map[20][20];
if(map[e.x][e.y]==0)
return OK;
else
return OVERFLOW;
}
Status FootPrint(PosType e)
{
//int map[20][20];
map[e.x][e.y]=7;
return OK;
}
Status MarkPrint(PosType e)
{
//int map[20][20];
map[e.x][e.y]=3;
return OK;
}
void PrintMaze()
{
//int map[20][20];
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(map[i][j]==0)
printf(" ");
else if(map[i][j]==1)
printf("1");
else if(map[i][j]==3)
printf("3");
else if(map[i][j]==7)
printf("7");
}
}
}
Status MazePath(PosType start,PosType end)
{
SqStack s;
PosType curpos;
InitStack(&s);
SElemType e;
int curstep;
curpos=start;
curstep=1;
do{
if(Pass(curpos))
{
e.di=1;
e.ord=curstep;
e.seat=curpos;
Push(&s,e);
if(curpos.x==end.x&&curpos.y==end.y)
printf("到達終點");
return TRUE;
curpos=NextPos(&curpos,1);
curstep++;
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(e.seat);
Pop(&s,&e);
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(&e.seat,e.di);
}
}
}
} while(!StackEmpty(s));
printf("不能到達終點");
return FALSE;
}
main()
{
SqStack s;
PosType start,end;
start.x=1;start.y=0;
end.x=18;end.y=19;
InitStack(&s);
Maze();
MazePath(start,end);
PrintMaze();
system("pause");
Maze();
MazePath(start,end);
PrintMaze();
system("pause");
return 0;
}
uj5u.com熱心網友回復:
void PrintMaze(){
//int map[20][20];
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(map[i][j]==0)
printf(" ");
else if(map[i][j]==1)
printf("1");
else if(map[i][j]==3)
printf("3");
else if(map[i][j]==7)
printf("7");
}
printf("\n");
}
}
uj5u.com熱心網友回復:
內回圈每輸出一行,輸出一個換行符。uj5u.com熱心網友回復:
我在創建迷宮那個模塊添加了
就只在上面換行
uj5u.com熱心網友回復:
列印迷宮的時候,每一行要換行。uj5u.com熱心網友回復:
記憶體里面二維陣列是連續存盤的,只是邏輯上是分行列,輸出二維陣列并不會自動換行。uj5u.com熱心網友回復:
除非你把控制臺視窗的寬度設定成二維陣列一行的寬度,輸出二維陣列才會自動換行。uj5u.com熱心網友回復:
謝謝你!解決了uj5u.com熱心網友回復:
大哥 我這在模塊中只能運行一次 在第一次判斷是否為終點那就停止運行了 是什么原因?
Status MazePath(PosType start,PosType end)
{
SqStack s;
PosType curpos;
InitStack(&s);
SElemType e;
int curstep;
curpos=start;
curstep=1;
do{
if(Pass(curpos))
{
e.di=1;
e.ord=curstep;
e.seat=curpos;
Push(&s,e);
if(curpos.x==end.x&&curpos.y==end.y)
printf("到達終點");
return TRUE;
curpos=NextPos(&curpos,1);
curstep++;
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(e.seat);
Pop(&s,&e);
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(&e.seat,e.di);
}
}
}
} while(!StackEmpty(s));
printf("不能到達終點");
return FALSE;
}
uj5u.com熱心網友回復:
程式邏輯你還是需要再檢查,或者單步運行、除錯,才知道哪里變數的值非預期地變了,或者什么情況沒有預計到。uj5u.com熱心網友回復:
好的 謝謝你!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69170.html
標籤:基礎類
上一篇:關于編譯openssl的問題
下一篇:如何確定原圖在拼接圖上的坐標
