// Name : 迷宫.cpp
// Author : mtt
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define M 8
#define N 11
typedef struct
{
int x;
int y;
int d;
}Datatype;
struct seqstack
{
int Maxsize;
int top;//指示栈定位置
Datatype *s;
};
typedef struct seqstack *pseqstack;
void createEmptystack(seqstack &s ,int m,int n)
{
s.Maxsize=m*n;
s.top=-1;
s.s=new Datatype[m*n];
}
void push_seq(pseqstack pastack, Datatype x)
{
if(pastack->top==pastack->Maxsize-1)
{
cout<<"stack is full";
exit(1);
}
pastack->top++;
pastack->s[pastack->top]=x;
}
Datatype pop(pseqstack pastack)
{
if(pastack->top==-1)
{cout<<"栈为空"<<endl;
exit (1);}
else
return pastack->s[pastack->top];
}
int is_Empty(pseqstack pastack)
{
if (pastack->top==-1)
return 1;
else return 0;
}
void mazepath(pseqstack pastack,int m,int n,int x1,int y1,int x2,int y2,int direct[][2],int maze[][11])
{
createEmptystack(*pastack,m,n);
int g,h;
int i,j,k;
maze[x1][y1]=2;//标记已经走过
Datatype element ;
element.x=x1;
element.y=y1;
element.d=-1;
push_seq(pastack,element);
while(!is_Empty(pastack))
{
element=pop(pastack);
i=element.x;
j=element.y;
k=element.d;
while (k<=3)
{
g=maze[i][k]+direct[k][0];
h=maze[j][k]+direct[k][1];
if(g==x2&&h==y2&&maze[g][h]==0)
{
cout<<"the reverse path is"<<endl;
cout<<"the node is"<<g<<","<<h<<" ";
cout<<"the node is"<<i<<","<<j<<" ";
while (!is_Empty(pastack))
{
element=pop(pastack);
cout<<""<<element.x<<","<<element.y<<" ";
}
free(pastack->s);
free(pastack);
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++;
}
}
cout<<"no path find"<<endl;
free(pastack->s);
free(pastack);
}
int main() {
int direct[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
seqstack s;
pseqstack p=&s;
int maze [M][N]={{1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1,1},
{1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,1,1,0,1,1},
{1,1,0,0,1,0,1,1,0,0,1},
{1,1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1}};
mazepath(p,8,11,1,1,6,9,direct,maze);
return 0;
}
原文链接: https://www.cnblogs.com/panlijiao/archive/2012/11/02/2773875.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/67961
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!