迷宫问题c++代码

来源:互联网 发布:excel 库存软件 编辑:程序博客网 时间:2024/05/01 18:14

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
#define LEN 12

typedef struct
{
 int x,y;
}postype;

typedef struct
{
 int m,n;
 int arr[LEN][LEN];
}mazetype;

typedef struct
{
 int ord;
 postype seat;
 int di;
}selemtype;
#include "sqstack.cpp"
void initmaze(mazetype &maze,int a[][LEN],int row,int col);
bool mazepath(mazetype &maze,postype start,postype end);
void print_mz(mazetype maze,int row,int col);
status pass(mazetype &maze,postype pos);
void footprint(mazetype &maze,postype pos,int curstep);
void markprint(mazetype &maze,postype pos);
int same(postype curpos,postype end);
postype nextpos(postype pos,int dir);
void readcommand(char &cmd);
void initialization();
void interpret(char cmd);


void main()
{
 
 char cmd;
 do
 {
     initialization();
     readcommand(cmd);
  interpret(cmd);
    }while(cmd!=&apos;q&apos;&&cmd!=&apos;Q&apos;);
}

void initialization()
{
    cout<<"求解迷宫---c,退出程序---q"<<endl;
}
void readcommand(char &cmd)
{
 do
 {
  cmd=getche();
 }while(cmd!=&apos;c&apos;&&cmd!=&apos;C&apos;&&cmd!=&apos;q&apos;&&cmd!=&apos;Q&apos;);
}


void interpret(char cmd)
{
 int rnum,cnum;//rnum表示行数,cnum表示列数
 int a2[LEN][LEN];
 int i,j;
 mazetype ma;
 postype from,term;
 switch(cmd)
 {
 case &apos;c&apos;:
 case &apos;C&apos;:
  
        FILE *in,*out;
  in=fopen("in.data","rb");
        out=fopen("out.data","wb");
  fscanf(in,"%d",&rnum);
  fscanf(in,"%d",&cnum);
  for (i=1;i<=rnum;i++)
   for(j=1;j<=cnum;j++)
          fscanf(in,"%d",&a2[i][j]);

  initmaze(ma,a2,rnum,cnum);
  

        cout<<"文件中读取的迷宫数据:"<<endl;
  
        for (i=1;i<=rnum;i++)
  {
      for(j=1;j<=cnum;j++)
          cout<<a2[i][j]<<setw(2);
   cout<<endl;
  }
  cout<<"建立迷宫:"<<endl;
  print_mz(ma,rnum,cnum);
 /*  break;
    case &apos;m&apos;:
 case &apos;M&apos;:*/
        cout<<"请输入迷宫入口和出口坐标位置:"<<endl;
  cin>>from.x>>from.y>>term.x>>term.y;
  
  if (mazepath(ma,from,term))
  {
   cout<<"求得迷宫路径如下:"<<endl;
      print_mz(ma,rnum,cnum);
  }
  else
      cout<<"该迷宫没有从给定的入口到出口的路径的信息!"<<endl;
  break;
  fclose(in);
        fclose(out);
/* case &apos;p&apos;:
 case &apos;P&apos;:
  print_mz(ma,rnum,cnum);*/
 }
}

void initmaze(mazetype &maze,int a[][LEN],int row,int col)
{
 int i,j;
 for(i=1;i<=row+1;i++)
  for(j=1;j<=col+1;j++)
      maze.arr[i][j]=a[i][j];
 for(j=0;j<=col+1;j++)
 {
  maze.arr[0][j]=1;
     maze.arr[row+1][j]=1;
 }
 for(i=0;i<=row+1;i++)
 {
  maze.arr[i][0]=1;
     maze.arr[i][col+1]=1;
 }
}

bool mazepath(mazetype &maze,postype start,postype end)
{
 sqstack s;
 selemtype e;
 postype curpos;
 int curstep;
 bool found;
 initstack(s);
 curpos=start;
 curstep=1;found=FALSE;
 do
 {
  if(pass(maze,curpos))
  {
   footprint(maze,curpos,curstep);
   e.ord=curstep;
   e.seat.x=curpos.x;
   e.seat.y=curpos.y;
   e.di=1;

   push(s,e);
   if(same(curpos,end)) found=TRUE;
   else
   {
    curpos=nextpos(curpos,1);
    curstep++;
   }//else
  }//if
  else
   if(!stackempty(s))
   {
    pop(s,e);
    while(e.di==4&&!stackempty(s))
    {
     markprint(maze,e.seat);
     pop(s,e);
     curstep--;
    }//while
    if(e.di<4)
    {
     e.di++;
     push(s,e);
     curpos=nextpos(e.seat,e.di);
    }//if
   }//if
 }while(!stackempty(s)&&!found);
 return found;
}//mazepath

void print_mz(mazetype maze,int row,int col)
{
 int i,j;
 for(i=0;i<=row+1;i++)
 {
  for(j=0;j<=col+1;j++)
      cout<<maze.arr[i][j]<<setw(2);
  cout<<endl;
 }
}

status pass(mazetype &maze,postype pos)
{
 return (maze.arr[pos.x][pos.y]==0);
}

void footprint(mazetype &maze,postype pos,int curstep)
{
 maze.arr[pos.x][pos.y]=curstep;
}

void markprint(mazetype &maze,postype pos)
{
 maze.arr[pos.x][pos.y]=-1;
}

postype nextpos(postype pos,int dir)
{
 postype direct[5]={{0,0},{0,1},{1,0},{0,-1},{-1,0}};
 pos.x +=direct[dir].x;
 pos.y +=direct[dir].y;
 return pos;
}

int same(postype curpos,postype end)
{
 if (curpos.x==end.x&&curpos.y==end.y) return OK;
 else return 0;
}

 

sqstack.cpp代码:

#include "predef.h"

 

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef  int status;
typedef struct
{
 selemtype *base;
 selemtype *top;
 int stacksize;
}sqstack;

status initstack(sqstack &s);
//status destroystack(sqstack&s);
//status clearstack(sqstack &);
status stackempty(sqstack s);
status push(sqstack &s,selemtype e);
status pop(sqstack &s,selemtype &e);

status initstack(sqstack &s)
{
 s.base=(selemtype *)malloc(STACK_INIT_SIZE*sizeof(selemtype));
 if(!s.base) exit(OVERFLOW);
 s.top=s.base;
 s.stacksize=STACK_INIT_SIZE;
 return OK;
}

status stackempty(sqstack s)
{
 return (s.top==s.base);
}

status push(sqstack &s,selemtype e)
{
 if(s.top-s.base>=s.stacksize)
 {
  s.base=(selemtype *)realloc(s.base,(s.stacksize +STACKINCREMENT)*sizeof(selemtype));
     if(!s.base) exit(OVERFLOW);
  s.top=s.base+s.stacksize;
  s.stacksize+=STACKINCREMENT;
 }
 *s.top++=e;
 return OK;
}

status pop(sqstack &s,selemtype &e)
{
 if(s.top==s.base) return ERROR;
 e=*--s.top;
 return OK;
}

 

 

测试数据格式in.data:

3 4
0 0 0 1
1 0 0 1
0 1 0 0

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 批量性不良再限度样本规格内怎么办 老公说老婆不攒钱都买衣服了怎么办 华为手机获取安装包信息失败怎么办 液压电动脱模器不上升了怎么办 退伍军人证和身份证名字不付怎么办 车辆有违章需要短信提醒要怎么办 1969年退伍的退伍证丢了怎么办 江苏移动没实名认证停机了怎么办呀 亿达老年手机来电音量太大怎么办 挑三十六乘二的内螺纹丝瞳毛怎么办 内六角螺丝的冒滑了怎么办 孔里的内六角滑了怎么办 十字螺丝刀的头卡在螺母里怎么办 内六角扳手断在螺丝孔里怎么办 六棱螺丝刀折进螺丝里了怎么办 内6棱螺丝拧花了怎么办 只有pe模式可以用键盘鼠标怎么办 电脑重做了系统连不上无线网怎么办 联想手机死机了怎么办不可拆卸电池 手机界面弹出后大小无法调整怎么办 方向盘打方向瑟瑟的吱吱响怎么办 三脚架提升杆螺丝滑丝了怎么办 花土里面有很多细长的螺丝怎么办 防盗门外边上边缝宽螺丝松了怎么办 填充墙与框架梁不对齐怎么办 亚轧滚珠丝杠螺帽超程了怎么办 在公司交的五险一金辞职了怎么办 铝合金的渣子整到眼睛里怎么办 铁锅手柄上的铆钉拧不动怎么办 喂完鱼鱼缸上边飘着一层油膜怎么办 铸铁管道横着排的结口漏水怎么办 缤智行李架免打孔螺丝款怎么办 前保险杠和叶子板缝隙大怎么办 新奥拓的大灯调节螺丝滑丝了怎么办 小米手环2计步不准怎么办 小米手环3计步不准怎么办 眼镜用洗发水洗了后模糊怎么办 雷朋近视镜眼镜腿折了怎么办 近视镜眼镜腿断了该怎么办 老师把学生的眼镜打坏了怎么办 生死狙击忘了密保改不了密码怎么办