迷宫问题 C++实现

#include"fstream"
#include
#include
using namespace std;
#define MAXSIZE 100
#define m 8
#define n 8
int maze[m+2][n+2];

栈的表示

typedef struct
{
 int i,j,di;
}Box;
typedef struct
{
 Box data[MAXSIZE];
 int top;
}StType;

迷宫函数

void MazePath(int xi,int yi,int xe,int ye)
{
 int i=0,j=0,a=1,k,di,find;
 StType s;
 s.top=-1;
 s.top++;
 s.data[s.top].i=xi;//栈顶元素
 s.data[s.top].j=yi;
 s.data[s.top].di=-1;
 maze[xi][yi]=-1;
 while(s.top>-1)
 {
  i=s.data[s.top].i;//取栈顶元素
  j=s.data[s.top].j;
  di=s.data[s.top].di;
  if(i==xe&&j==ye)
  {getchar();
   cout<<"找到第"<

从.txt文件中读取迷宫(迷宫以存放在此文件中)

例如
1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1

void file(const char *filename,int a[m+2][n+2])
{
 ifstream infile(filename,ios::in);
 if(!infile)
 {
  cerr<<"open error!"<>a[i][j];
 }
 infile.close();
}

主函数

int main()
{
 int t=0;
 string fn;
 cout<<"输入存放迷宫的文件地址(.txt文件):";
 cin>>fn;
 file(fn.c_str(),maze);
 cout<<"迷宫:\n";
 for(int i=0;i

你可能感兴趣的:(原创)