#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
typedef int Status;
typedef struct {
int x, y;
} PosType;
typedef struct {
int ord;
PosType seat;
int di;
} SElemType;
typedef struct {
SElemType *top;
SElemType *base;
int stacksize;
} SqStack;
/************* STACK **************/
Status CreateStack(SqStack &s) {
s.base = (SElemType *) malloc (STACK_INIT_SIZE * (sizeof(SElemType)));
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return OK;
}
Status isEmpty(SqStack s) {
if (s.top == s.base)
return 1;
return 0;
}
Status Push(SqStack &s, SElemType e) {
if (s.top - s.base >= s.stacksize) {
s.base = (SElemType *) realloc (s.base, (STACK_INIT_SIZE + s.stacksize) * sizeof(SElemType));
if (!s.base) return ERROR;
s.top = s.base + s.stacksize;
s.stacksize += STACK_INIT_SIZE;
}
*s.top++ = e;
return OK;
}
SElemType Pop(SqStack &s) {
if (!isEmpty(s)) {
return *--s.top;
} else printf("Empty error!\n");
}
SElemType GetTop(SqStack s) {
if (!isEmpty(s)) {
return *--s.top;
} else printf("Empty error!\n");
}
/************* STACK **************/
/************* BEGIN MAZE AGORITHM **************/
int maze[][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1, 1, 0, 1, 0},
{0, 1, 1, 0, 1, 1, 1, 0, 1, 0},
{0, 1, 1, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 0, 0, 0, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 0},
{0, 1, 0, 1, 1, 1, 0, 1, 1, 0},
{0, 1, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
/* 判断是否已经走过或者已经是死胡同 */
bool isMarked(PosType pos) {
if (maze[pos.x][pos.y] > 1)
return 1;
return 0;
}
/* 判断是否走通 */
bool CanPass(PosType pos) {
if (maze[pos.x][pos.y] == 0 || isMarked(pos))
return 0;
return 1;
}
/* 按顺时针改变方向 */
PosType getNext(PosType pos, int di) {
switch (di) {
case 1: pos.y++; break;
case 2: pos.x++; break;
case 3: pos.y--; break;
case 4: pos.x--; break;
default: break;
}
return pos;
}
/* 如果迷宫当前位置的值为2为就说明是已经走过的 */
Status FootPrint(PosType &pos) {
maze[pos.x][pos.y] = 2;
return OK;
}
/*同上,3为死胡同*/
Status MarkPrint(PosType &pos) {
maze[pos.x][pos.y] = 3;
return OK;
}
bool MazePath(SqStack s) {
SElemType e;
PosType start, end, curpos;
start.x = 1;
start.y = 1;
end.x = 8;
end.y = 8;
curpos = start;
int curstep = 1;
do {
if (CanPass(curpos)) {
FootPrint(curpos);
e.ord = curstep;
e.seat = curpos;
e.di = 1;
Push(s, e);
if ((curpos.x == end.x) && (curpos.y == end.y)) return 1;
curpos = getNext(curpos, 1);
curstep++;
} else {
if (!isEmpty(s)) {
e = Pop(s);
while (e.di == 4 && !isEmpty(s)) {
MarkPrint(e.seat);
e = Pop(s);
}
if (e.di < 4) {
e.di++; Push(s, e);
curpos = getNext(e.seat, e.di);
}
}
}
} while (!isEmpty(s));
return 0;
}
/************* END MAZE AGORITHM **************/
int main() {
SqStack S;
CreateStack(S);
if (MazePath(S))
printf("Congratulations! Can pass!\n");
else printf("!!!!!!!! OH~~ CAN NOT PASS !!!!!!!!\n");
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (maze[i][j] == 2) printf("* ");
else printf("%d ", maze[i][j]);
}
printf("\n");
}
}
枚举法求迷宫
最新推荐文章于 2024-05-19 16:56:28 发布