迷宫问题

来源:互联网 发布:linux tail刷新 编辑:程序博客网 时间:2024/05/02 01:17

/*求迷宫中路径的问题*/
/*程序中用0表示迷宫中的不可通通道块,
用1表示迷宫中的可通通道块,用5表示入
过栈且又出栈的元素,用9表示路径*/


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAXLEN 10
#define MAXSIZE 100
typedef struct
{
    int row;
    int col;
}postype;           /*用来表示在迷宫中的坐标*/
typedef struct
{
    int a[MAXLEN][MAXLEN];
    int row;
    int col;
}mazetype;    /*迷宫的类型*/
typedef struct  
{
    int org;       /*通道块在路径上的“序号”*/
    postype seat;  /*通道块在迷宫中的“坐标位置”*/
    int di;        /*从此通道块到下一个通道块的方向(1:左,2:右,3:下,4:上,)*/
}selemtype;        /*栈元素类型*/ 
typedef struct
{
    selemtype *top;  /*指向栈顶元素的指针*/
    selemtype *base; /*指向栈底元素的指针*/
    int stacksize;   /*栈中已经分配的储存空间*/
}stack;
mazetype m1;
stack s1;
postype start_1;
postype end_1;

 


/*创建一个空栈的函数*/
void create_s(stack *s)
{
    s->base=(selemtype *)malloc(MAXSIZE *sizeof(selemtype));
    if(!s->base)exit(1);
    s->top=s->base;
    s->stacksize=MAXSIZE;
}

/*入栈函数*/
void push(stack *s,selemtype e)
{
    if(s->top-s->base>=s->stacksize)exit(1);
    *(s->top)=e;
    s->top++;
}

/*出栈函数*/
void pop(stack *s)
{
    if(s->top==s->base)exit(1);
    s->top--;
}

/*求栈中元素的下一个通道块是否可通或被入过栈的函数,如果可通且没入过栈则返回 1 .*/
int pass_next(stack *s,mazetype m,int i)
{
switch(i)
  {
    case 1:if(m.a[(s->top-1)->seat.row-1][(s->top-1)->seat.col-1-1]==1)return 1;else return 0;
    case 2:if(m.a[(s->top-1)->seat.row-1][(s->top-1)->seat.col-1+1]==1)return 1;else return 0;
    case 3:if(m.a[(s->top-1)->seat.row-1+1][(s->top-1)->seat.col-1]==1)return 1;else return 0;
    case 4:if(m.a[(s->top-1)->seat.row-1-1][(s->top-1)->seat.col-1]==1)return 1;else return 0;
    default:printf("error/n");exit(1); 
  }
}

/*求栈中最后的元素的下一个通道块位置并赋值给栈的元素类型返回的函数*/
selemtype pos_next(stack *s,int i)
{
    selemtype p;
    switch(i)
    {
 case 1:p.seat.row=(s->top-1)->seat.row;p.seat.col=(s->top-1)->seat.col-1;p.org=(s->top-1)->org+1;return p;
 case 2:p.seat.row=(s->top-1)->seat.row;p.seat.col=(s->top-1)->seat.col+1;p.org=(s->top-1)->org+1;return p;
 case 3:p.seat.row=(s->top-1)->seat.row+1;p.seat.col=(s->top-1)->seat.col;p.org=(s->top-1)->org+1;return p;
 case 4:p.seat.row=(s->top-1)->seat.row-1;p.seat.col=(s->top-1)->seat.col;p.org=(s->top-1)->org+1;return p;
    default:printf("error/n");exit(1);
    }
}

/*将栈中存放的路径画到迷宫矩阵的函数*/
void display_m(stack *s,mazetype *m)
{
    for(;(s->base)<(s->top);++s->base)
    {m->a[s->base->seat.row-1][s->base->seat.col-1]=5;}
}


/*把起始位置放入栈中的第一个位置的函数*/
void csh_stack(stack *s,postype start,mazetype *m)
{
 selemtype s_ele;
 s_ele.org=1;
 s_ele.seat.row=start.row;
 s_ele.seat.col=start.col;
 m->a[start.row-1][start.col-1]=9;
 push(s,s_ele);
}

/*将找到保存到栈中的路径打印到迷宫里的函数*/
void display(stack *s,mazetype *m)
{
 void print(mazetype *m);
 for(;s->base<s->top;++s->base)
  m->a[s->base->seat.row-1][s->base->seat.col-1]=9;
 print(m);
}

 

 

/*创建一个迷宫的函数*/
void create_m(mazetype *m)
{
    int num,i;
  /*初始化迷宫的外围-——第一行元素为0,表示不可通*/
     m->row=0;        
   if(m->row==0)
     {for(m->col=0;m->col<MAXLEN;m->col++)
       m->a[m->row][m->col]=0;}
  /*初始化迷宫的外围-——第MAXLEN-1行元素为0,表示不可通*/
       m->row=MAXLEN-1;  
   if(m->row==MAXLEN-1)
     {for(m->col=0;m->col<MAXLEN;m->col++)
       m->a[m->row][m->col]=0;}
  /*初始化迷宫的外围-——第一列元素为0,表示不可通*/
    m->col=0;    
   if(m->col==0)
     {for(m->row=0;m->row<MAXLEN;m->row++)
       m->a[m->row][m->col]=0;}
  /*初始化迷宫的外围-——第MAXLEN-1列元素为0,表示不可通*/
     m->col=MAXLEN-1;  
   if(m->col==MAXLEN-1)
     {for(m->row=0;m->row<MAXLEN;m->row++)
       m->a[m->row][m->col]=0;}
 
  /*初始化迷宫除外墙以外的内部为1,表示可通*/
 for(m->row=1;m->row<MAXLEN-1;m->row++) 
     for(m->col=1;m->col<MAXLEN-1;m->col++)
         m->a[m->row][m->col]=1;
  /*输入迷宫中的不可通通道块*/
 printf("输入不可通通道块的个数:");
     scanf("%d",&num);
 for(i=0;i<num;++i)
   {
     printf("输入不可通通道块在迷宫中的横坐标:");
     scanf("%d",&m->row);
     printf("输入不可通通道块在迷宫中的纵坐标:");
     scanf("%d",&m->col);
     m->a[m->row-1][m->col-1]=0;
   }
}

/*输出迷宫的函数*/
void print(mazetype *m)
{

    printf("迷宫如下:/n");
    for(m->row=0;m->row<MAXLEN;++m->row)
      {
        for(m->col=0;m->col<MAXLEN;++m->col)
            printf("%d ",m->a[m->row][m->col]);
            printf("/n");
      }
}

/*在迷宫中找路的函数*/
void mazepath(stack *s1,mazetype *m,postype end)
{
    int i;       /*用来控制通向下一个通道块的方向*/
 selemtype ele;
      for(i=1;i<=4;++i)
          {
     ele=pos_next(s1,i);
     if(pass_next(s1,*m,i)&&(ele.seat.row!=end.row||ele.seat.col!=end.col))
              {
                push(s1,ele);
                m->a[ele.seat.row-1][ele.seat.col-1]=5;  /*表示此元素以入过栈了*/
                mazepath(s1,m,end);
              }
     if(ele.seat.row==end.row&&ele.seat.col==end.col)
     {push(s1,ele);return;}
          }
    pop(s1);
 mazepath(s1,m,end);
}

void main()
{
 start_1.row=2;     /*定义迷宫的入口*/
 start_1.col=2;
 end_1.row=9;
 end_1.col=9;       /*定义迷宫的出口*/
 end_1.col=9;
 create_s(&s1);     /*创建一个栈*/
 create_m(&m1);     /*创建一个迷宫*/
 print(&m1);        /*输出刚输入的迷宫*/
 csh_stack(&s1,start_1,&m1);  /*将入口的位置赋值给栈元素,并入栈*/
 mazepath(&s1,&m1,end_1);     /*寻找路径*/
 display(&s1,&m1);          /*输出找到路径后的迷宫*/

原创粉丝点击