迷宫问题
数组传递错误???
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int x,y,r;
/*line、list、dir分别为数组元素的行坐标、列坐标、choice数组的下标值即对应方向*/
}
DataType;
struct SeqStack
{
int MAXNUM;
int t;
DataType *s;
};
typedef struct SeqStack *PSeqStack;
PSeqStack create_EmptySeqStack(int n)
{
PSeqStack p=(PSeqStack)malloc(sizeof(struct SeqStack));
if(p!=NULL)
{
p->s=(DataType*)malloc(sizeof(DataType)*n);
if(p->s)
{
p->MAXNUM=n;
p->t=-1;
return p;
}
else free(p);
}
printf("Out of Space!\n");
return NULL;
}
int createmaze(int a[100][100],int m,int n)
{
int x,y;
for(x=0;x<m;x++)
a[x][0]=1;a[x][n-1]=1;
for(y=0;y<n;y++)
a[0][y]=1;a[m-1][y]=1;
printf("请输入内部迷宫组织,可走通的路径点为0,走不通为1.\n");
for(x=1,y=1;x<m-1,y<n-1;x++,y++)
scanf("%d",&a[x][y]);
}
int is_EmptyStack(PSeqStack p)
{
return(p->t==-1);
}
void pop(PSeqStack p)
{
if(p->t==-1) printf("Underflow!\n");
else p->t=p->t-1;
}
DataType top(PSeqStack p)
{
if(p->t==-1) printf("Stack is empty!\n");
else return(p->s[p->t]);
}
void push(PSeqStack p,DataType x)
{
if(p->t>=(p->MAXNUM)-1) printf("Overflow!\n");
else
{
p->t=p->t+1;
p->s[p->t]=x;
}
}
void mazepath(int *maze[],int *direction[],int x1,int y1,int x2,int y2,int M,int N)
{
int i,j,k;
int g,h;
PSeqStack st;
DataType element;
st=create_EmptySeqStack(M*N);
maze[x1][y1]=2;
element.x=x1;
element.y=y1;
element.r=-1;
push(st,element);
while(!is_EmptyStack(st))
{
element=top(st);
pop(st);
i=element.x;
j=element.y;
k=element.r+1;
while(k<=3)
{
g=i+direction[k][0];
h=j+direction[k][1];
if(g==x2&&h==y2&&maze[g][h]==0)
{
printf("The reverse path is:\n");
while(!is_EmptyStack(st));
{
element=top(st);
pop(st);
printf("the node is:%d%d\n",element.x,element.y);
}
return;
}
if(maze[g][h]==0)
{
maze[g][h]=2;
element.x=i;
element.y=j;
element.r=k;
push(st,element);
i=g;
j=h;
k=-1;
}
k=k+1;
}
}
printf("The path has not been found.\n");
}
int main()
{
int a,b,c,d,i,j;
int s=0;
int sta1,sta2,end1,end2;
int maze[100][100];
printf("确定迷宫大小:请输入迷宫长、宽分别为(长宽均不大于100):\n");
scanf("%d %d",&a,&b);
createmaze(maze,a,b);
printf("请输入入口、出口坐标为:\n");
scanf("%d %d %d %d",&sta1,&sta2,&end1,&end2);
maze[end1][end2]=0; /*出口可走通*/
int choice[8][2]={0,1,1,0,0,-1,-1,0,1,1,-1,1,1,-1,-1,-1};
/*每点处下一走向有8种选择,数组元素对应与i或j相加的值,每一行的两个数组元素对应东南西北、东南、东北、西南、西北8个方向。*/
mazepath(maze,choice,sta1,sta2,end1,end2,a,b);
}