迷宫。。。

来源:互联网 发布:常见网络拓扑结构图 编辑:程序博客网 时间:2024/04/28 04:51
//============================================================================
// 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;
}


原创粉丝点击