// Maze.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include <iostream.h>
#include <string.h>
#define Edge 20
#define PLUS 5
#define STA_SIZE 100
#define Startx 1
#define Starty 1
typedef struct Store{
int x, y, d, lsx, lsy;
bool nl;
} Store, *Link; // 储存路径
Store InitStore (Store &d) {
d.x = d.lsx = Startx;
d.y = d.lsy = Starty;
d.d = 4;
d.nl = false;
return d;
}
typedef struct {
Link base;
Link top;
int stacksize;
int count;
bool array[Edge*Edge];
} SqStack;
bool Push(SqStack &S, Store &e)
{
if(S.top - S.base >= S.stacksize) {
S.base = (Link) realloc (S.base, (S.stacksize + PLUS) * sizeof (Store));
if (!S.base) return false;
S.top = S.base + S.stacksize;
S.stacksize += PLUS;
}
*(++S.top) = e;
S.count++;
return true;
}
bool Pop(SqStack &S, Store &e) {
if(S.top == S.base) return false;
e = *(--S.top);
S.count--;
return true;
}
bool InitStack (SqStack &S) {
S.base = (Link) malloc (STA_SIZE * sizeof(Store));
if(!S.base) return false;
S.top = S.base;
S.stacksize = STA_SIZE;
S.count = 0;
for(int i = 0; i<Edge*Edge;i++)
S.array[i] = false;
return true;
}
bool DestroyStack (SqStack &S) {
if(!S.base) return false;
free(S.top);
return true;
}
bool NextPos (int a[], SqStack &S, Store &e)
{
for(; e.d>=0; )
{
switch(e.d)
{
case 4:
// right
// 方向减1,消除走不通的方向
e.d--;
// 如果下一步为路(0)或出口(-1),且如果为路,不是当前位置的前一位置,那么确认并记录下当前试探位置为后一位置
// 如果条件不满足,即是墙,则判断下一方向
if(a[S.top->x * Edge + (S.top->y + 1)] <= 0 && S.top->lsy != (e.y + 1) && S.array[S.top->x * Edge + (S.top->y + 1)] == false)
{
e.y++;
S.array[S.top->x * Edge + (S.top->y + 1)] = true;
return true;
}
break;
case 3:
// down
e.d--;
if(a[(S.top->x + 1) * Edge + S.top->y] <= 0 && S.top->lsx != (e.x + 1) && S.array[(S.top->x + 1) * Edge + S.top->y] == false)
{
e.x++;
S.array[(S.top->x + 1) * Edge + S.top->y] = true;
return true;
}
break;
case 2:
// left
e.d--;
if(a[S.top->x * Edge + (S.top->y - 1)] <= 0 && S.top->lsy != (e.y - 1) && S.array[S.top->x * Edge + (S.top->y - 1)] == false)
{
e.y--;
S.array[S.top->x * Edge + (S.top->y - 1)] = true;
return true;
}
break;
case 1:
// up
e.d--;
if(a[(S.top->x - 1) * Edge + S.top->y] <= 0 && S.top->lsx != (e.x - 1) && S.array[(S.top->x - 1) * Edge + S.top->y] == false)
{
e.x--;
S.array[(S.top->x - 1) * Edge + S.top->y] = true;
return true;
}
break;
default:
S.top->nl = true;
return false;
//*/
}
}
return false;
}
void Patch (int a[], Store &d, SqStack &S)
{
while(a[S.top->x*Edge + S.top->y] != -1)
{
if(NextPos(a, S, d))
{
Push(S, d);
(S.top-1)->d = S.top->d;
S.top->d = d.d = 4;
d.lsx = S.top->x;
d.lsy = S.top->y;
}
else
{
while(S.top->nl)
{
Store td;
Pop(S, td);
d = *S.top;
d.lsx = d.x;
d.lsy = d.y;
}
if(S.top->x == Startx && S.top->y == Starty)
{
cout << "error! 迷宫无解" << endl;
return;
}
}
}
}
void printdir (Link top, char dir[])
{
switch(top->d)
{
case 3:
strcpy(dir,"→");
break;
case 2:
strcpy(dir,"↓");
break;
case 1:
strcpy(dir,"←");
break;
case 0:
strcpy(dir,"↑");
}
}
void PrintMaze (SqStack &S) {
SqStack Sq;
InitStack(Sq);
for(;S.top != S.base;S.top--)
{
*(++Sq.top) = *S.top;
}// 倒序入栈
Sq.count = 1;
// 用函数中建立的新栈来顺序输出结果
cout <<"/n 1表示死路,0表示通路,-1表示出口,9表示边界!";
cout << "/n/n/n";
cout << "/t行走路径如下:/n/t";
for(;Sq.top != Sq.base;Sq.top--,Sq.count++)
{
char dir[10];
printdir(Sq.top, dir);
cout <<"(" << Sq.top->x << "," << Sq.top->y << ") " << ":" << dir <<"/t";
if(Sq.count%4 == 0)
{
cout << endl;
cout << "/t";
}
}
cout <<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