当前位置:编程学习 > C/C++ >>

需一个栈解迷宫的c++程序

答案:
终于编完了

#include<iostream>
#include<string>
#include<fstream>
void attemp(char*,char*,int,int*); //走迷宫
void cancell(char*,char*,int,int*); //走错返回
int exit_p=0; //用于判断是否已到出口
using namespace std;

void main()
{
int i,j,sizemaze_x,sizemaze_y,*maze_copy,size;
char*point,*point_star,*point_e,*point_star1,*point_filename;
char* position_e_1(char*,int);
string filename;
cout<<"input the file path:";
cin>>filename;
point_filename=&filename[0];
ifstream in(point_filename,ios::in);

try{
if(in.fail()) throw 1;
}
catch(int)
{
cout<<"Error:Can not open file"<<endl;
exit(1);
}
/*判断指定文件是否存在*/
string s;
getline(in,s);
sizemaze_y=(s[0]-48)*10+s[1]-48+2;
sizemaze_x=(s[5]-48)*10+s[6]-48+2;
size=sizemaze_x*sizemaze_y;
point=new char[size]; //申请存放迷宫数据的空间
maze_copy=new int[size]; //申请用于判断该点岔口情况的数据空间
int*maze_copy1=maze_copy;
int*pp=maze_copy;

for(i=0;i<size;i++,maze_copy1++)
*maze_copy1=0; //初始数据

point_star1=point_star=point;

for(i=0;i<sizemaze_x;i++)
{
*point='%';
point++;
} //初始扩充行
while(getline(in,s))
for(i=0,j=0;i<sizemaze_x;i++)
{
if((i%sizemaze_x==0)||(i%sizemaze_x==sizemaze_x-1)) *point='%';
else
{
*point=s[j];
j++;
}
point++;
} //输入迷宫数据
for(i=0;i<sizemaze_x;i++)
{
*point='%';
point++;
} //初始扩充行

point_e=position_e_1(point_star,size);//寻找e
attemp(point_e,point_e,sizemaze_x,maze_copy);
for(i=0;i<size;i++,point_star++)
if(*point_star=='9') *point_star='.';
point_star-=size; //转化为要求输出
for(i=2,point_star=point_star+sizemaze_x;i<sizemaze_y;i++)
{
for(j=0;j<sizemaze_x;j++)
{
if(j%sizemaze_x!=0 && j%sizemaze_x!=sizemaze_x-1)
cout<<*point_star;
point_star++;
}
cout<<endl;
} //输出迷宫
delete point_star1;
}
/*寻找开始点*/
char* position_e_1(char* x,int couts)
{
for(int i=0;i<couts;i++)
if(x[i]=='e') break;
return &x[i];
}
/*找路径*/
void attemp(char* nowposition,char* nowposition_star,int sizemaze_x,int* maze_copy)
{
int r=0,d=0,l=0,u=0; //用于判断方向
if(*(nowposition+1)=='x' || *(nowposition+sizemaze_x)=='x' || *(nowposition-1)=='x' || *(nowposition-sizemaze_x)=='x')
exit_p=1;
if(*(nowposition+1)=='.') r=1;
if(*(nowposition-1)=='.') l=1;
if(*(nowposition+sizemaze_x)=='.') d=1;
if(*(nowposition-sizemaze_x)=='.') u=1;
if((r+d+l+u)>1 && exit_p!=1) //计算和填写岔口数据
{
int* poi_maze_copy;
poi_maze_copy=maze_copy;
poi_maze_copy+=nowposition-nowposition_star;
*poi_maze_copy=r+d+l+u;
}
/*判断四个方向和跳到下一步*/
if(r==1 && exit_p!=1)
{
*(nowposition+1)='+';
attemp(nowposition+1,nowposition_star,sizemaze_x,maze_copy);

}
if(d==1 && exit_p!=1)
{
*(nowposition+sizemaze_x)='+';
attemp(nowposition+sizemaze_x,nowposition_star,sizemaze_x,maze_copy);
}
if(l==1 && exit_p!=1)
{
*(nowposition-1)='+';
attemp(nowposition-1,nowposition_star,sizemaze_x,maze_copy);
}
if(u==1 && exit_p!=1)
{
*(nowposition-sizemaze_x)='+';
attemp(nowposition-sizemaze_x,nowposition_star,sizemaze_x,maze_copy);
}
/*如果是死路回到上一个岔口*/
if((r+d+l+u)==0 && exit_p!=1)
cancell(nowposition,nowposition_star,sizemaze_x,maze_copy);

}
/*返回函数,已经走过的用‘9’表示*/
void cancell(char* nowposition,char* nowposition_star,int sizemaze_x,int* maze_copy)
{
int* point_maze_copy;
if(*(nowposition+1)=='+')
{
point_maze_copy=maze_copy;
point_maze_copy+=nowposition-nowposition_star;
if(*point_maze_copy<1)
{
if(*(point_maze_copy+1)>=1)
*(point_maze_copy+1)-=1; //判断上一步的路口岔口情况
*nowposition='9';
cancell(nowposition+1,nowposition_star,sizemaze_x,maze_copy);
}

上一个:c++存入,取出txt文本,
下一个:C++ Builder通过WMI获取系统信息

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,