📄 2.cpp
字号:
#include"2.h"
//extern chess nodle[25][25];
extern ppchess nodle;
extern int i;
extern int j;
extern int c_size;
extern p_people head;
///////////////////////////////////////////////////
///////////////随机产生一幅地图
void creat_map(int level)
{
int creat_susseed=OK;
do///////////////////////////////////保证了一定能产生出走的通的地图 v
{
chess_size(nodle, c_size);///////////随机产生一个地图 v
srand((unsigned)time(NULL));
for(i=0;i<c_size;i++)
{
for(j=0;j<c_size;j++)
{
if(i==0||i==c_size-1||j==0||j==c_size-1)
{
nodle[i][j].foot=YES; nodle[i][j].way_wall=WALL;
continue;
}
if(rand()%100<level)
{
nodle[i][j].foot=NO;nodle[i][j].way_wall=WAY;
}
else
{
nodle[i][j].foot=YES;nodle[i][j].way_wall=WALL;
}
}
}
nodle[1][1].foot=NO; nodle[1][1].way_wall=WAY;
nodle[c_size-2][c_size-2].foot=NO; nodle[c_size-2][c_size-2].way_wall=WAY;
////////////////////////////////////////////// 随机产生一个地图^
////////////////////////////////////////////////////////////////////////////////////////
if(solve_maze(head)==ERROR)////////////一定产生出走的通的地图条件
{
for(int i=0;i<c_size;i++)
{
delete [] nodle[i];
}
delete [] nodle;
creat_susseed=ERROR;
}
else creat_susseed=OK;
}while(creat_susseed==ERROR);/////////////////////保证了一定能产生出走的通的地图^
}
/////////////////////////////////////////
/////////////////显示地图
void show_map(int b)
{
int x;
int y;
if(b!=0)
{
::Sleep(b);
system("cls");
}
for(x=0;x<c_size;x++)
{
for(y=0;y<c_size;y++)
{
cout<<(char)nodle[x][y].way_wall<<" ";
}
cout<<endl;
}
}
////////////////////////////////////////////
///////////////////解决迷宫问题
status solve_maze(p_people &head)
{
///////////////////////以下是初使化栈空间
init(head);
///////////////////////解决迷宫
do
{
if(pass(head)==OK)///////////当前位置是否可通
{////////////////进栈
enter_zhan(head);//show_map();
}
else
{
if(other_pass(head)==OK)////////////是否还有其它方向没有扫描
{//////////////扫描其它方向
head->next->direction++;
}
else
{//////////////退栈
if(exit_zhan(head)==ERROR) { delete head ;return ERROR;}
}
}
}while(i!=c_size-2||j!=c_size-2);
return OK;
}
//////////////////////////////////////////////////////////
////////////////////判断当前位置是否可通
status pass(p_people &head)
{
int direc;
direc=head->next->direction;
switch(direc)
{
case 1:j=j+1;break;
case 2:i=i+1;break;
case 3:j=j-1;break;
case 4:i=i-1;break;
default:cout<<"严重错误1!!!"<<endl;
}
if(nodle[i][j].foot==NO&&nodle[i][j].way_wall==WAY)
return OK;
switch(direc)
{
case 1:j=j-1;break;
case 2:i=i-1;break;
case 3:j=j+1;break;
case 4:i=i+1;break;
default:cout<<"严重错误2!!!"<<endl;
}
return ERROR;
}
////////////////////////////////////////////////////////
///////////////////////////进栈函数
void enter_zhan(p_people &head)
{
nodle[i][j].foot=YES;
p_people s;
s=new people;
if(s==NULL) { cout<<"没有更多的空间可以申请"<<endl;exit(0);}
s->postion=i*100+j;
if(abs(head->next->postion-1)==2)
{
s->direction=2;
}
else s->direction=1;
s->next=head->next;
head->next=s;
s=NULL;
delete s;
}
///////////////////////////////////////////////////////
///////////////////////判断是否还有其它方向没有扫描函数
status other_pass(p_people &head)
{
if(head->next->direction<4)
{return OK;}
return ERROR;
}
//////////////////////////////////////////////////////////
/////////////////////退栈函数
status exit_zhan(p_people &head)
{
if(head->next->next==NULL) return ERROR;
p_people k;
k=head->next;
head->next=k->next;
switch(k->next->direction)
{
case 1:j=j-1;break;
case 2:i=i-1;break;
case 3:j=j+1;break;
case 4:i=i+1;break;
default:cout<<"严重错误3!!!"<<endl;
}
delete k;
return OK;
}
/////////////////////////////////////
///////////遍历栈空间函数
void cartoon_showmap(p_people &head)
{
int z,x;
float time;
p_people p;
p=head;
cout<<"现在请你输入每一步的时间间隔 (单位为秒. 精确到微秒)"<<endl;
do
{
cin>>time;
if(time<0.001)
{
cout<<"对不起你输入的时间低于1微秒,请你再输入一次"<<endl;
}
}while(time<0.001);
do
{
show_map(time*1000);
p=p->next;
x=p->postion%100;
z=(p->postion-x)/100;
nodle[z][x].way_wall=FOOT_WAY;
}while(p->next!=NULL);
nodle[1][1].way_wall=FOOT_WAY;
show_map(time*1000);
}
////////////////////////////////////////////////////////
//////////////////以下是初使化栈空间函数
void init(p_people &head)
{
i=1;j=1;
head=new people;
head->next=NULL;
p_people g;
g=new people;
g->direction=1;
g->postion=101;
g->next=head->next;
head->next=g;
g=NULL;
delete g;
}
///////////////////////////////////////////////////////////////////////
/////////////////定义棋盘的大小
void chess_size(ppchess &nodle,int c_size)
{
nodle=new pchess[c_size];
for(int l=0;l<c_size;l++)
{
nodle[l]=new chess[c_size];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -