不用多说,迷宫问题。
#include <stdlib.h>
#include <stdio.h>
//#include "SeqStack.h"
typedef struct
{
int x,y,d;
}
DataType;
struct SeqStack
{
int MAXNUM;
int t;
DataType *s;
};
typedef struct SeqStack *PSeqStack;
//创建一个空栈
PSeqStack createEmptyStack_seq(int m)
{
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if(pastack != NULL)
{
pastack->s = (DataType*)malloc(sizeof(DataType)*m);
if(pastack->s)
{
pastack->MAXNUM = m;
pastack->t = -1;
return pastack;
}
else free(pastack);
}
printf("Out of space!!\n");
return NULL;
}
//判断栈是否为空
int isEmptyStack_seq(PSeqStack pastack)
{
return (pastack->t==-1);
}
//压栈操作
void push_seq(PSeqStack pastack,DataType x)
{
if(pastack->t >= pastack->MAXNUM-1)
{
printf("Overflow!\n");
}
else
{
pastack->t = pastack->t+1;
pastack->s[pastack->t] = x;
}
}
//弹栈操作
void pop_seq(PSeqStack pastack)
{
if(pastack->t == -1)
{
printf("Underflow!\n");
}
else
pastack->t = pastack->t - 1;
}
//返回栈顶元素
DataType top_seq(PSeqStack pastack)
{
//DataType dt;
//dt.d=0;
if(pastack->t == -1)
{
printf("It is empty!\n");
//return dt;
}
else
return pastack->s[pastack->t];
}
void print(int maze[][11])
{
printf("\t\t-1\t");
for(int m=0;m<11;m++)
printf("%d ",m);
printf("\n");
for(m=0;m<11;m++)
{
printf("\n\t\t%d\t",m);
for(int n=0;n<11;n++)
if(maze[m][n]==0)
printf("□");
else if(maze[m][n]==3)
printf("■");
else if(maze[m][n]==2)
printf("▲");
else
printf("○");
}
printf("\n\t\t\t");
}
void mazePath(int maze[][11],int direction[][2],int x1, int y1, int x2, int y2, int M, int N)
{
PSeqStack pastack = createEmptyStack_seq(M*N);
int i,j,k;
int g,h;
DataType element;
element.x = x1;
element.y = y1;
element.d = -1;
maze[x1][y1] = 2;
push_seq(pastack,element);
while(!isEmptyStack_seq(pastack))
{
element = top_seq(pastack);
pop_seq(pastack);
i = element.x;
j = element.y;
k = element.d+1;
while(k <= 3)
{
g = i + direction[k][0];
h = j + direction[k][1];
if((maze[g][h]==0) && (g==x2) && (h==y2))
{
printf("The revers path is:\n");
printf("the node is:%d,%d\n",g,h);
printf("the node is:%d,%d\n",i,j);
maze[g][h]=3;
maze[i][j]=3;
while(!isEmptyStack_seq(pastack))
{
element = top_seq(pastack);
pop_seq(pastack);
printf("the node is:%d,%d\n",element.x,element.y);
maze[element.x][element.y]=3;
}
print(maze);
return;
}
if(maze[g][h]==0)
{
maze[g][h] = 2;
element.x = i;
element.y = j;
element.d = k;
push_seq(pastack,element);
i = g;
j = h;
k = -1;
}
k++;
}
}
printf("The path has not been found!\n");
}
void main()
{
// 0 1 2 3 4 5 6 7 8 9 10
int maze[11][11] = {{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//0
{ 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1 },//1
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1 },//2
{ 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1 },//3
{ 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1 },//4
{ 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1 },//5
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },//6
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },//7
{ 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1 },//8
{ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1 },//9
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } //10
};
int direction[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}};
mazePath(maze,direction,1,1,8,9,11,11);
}