📄 maze.h
字号:
// Copyright: 胡小民,丁展 2002.5
#ifndef _MAZE_H
#define _MAZE_H
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
#include "fstream.h"
#include "mazeMake.h"
#include "head.h"
// tell others that this path cannot be accessed in fact it describe the new maze
bool found=false;
int step=0;
class element;
class Stack;
class offsets;
int Max(int x,int y ,int z,int w);
void GenerateMaze();
long SearchPath(Stack& _stack);
void InitMark();
void ShowMaze();
void SimpleMaze();
void SubAllocate(int count,Stack _stackarray[],int n,element position,
int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],
int pos[CONST_MAZEX][CONST_MAZEY],int &tag);
void Allocate(Stack _stackarray[],int n,int type);
void HumanSubAllocate(int count,Stack _stackarray[],int n,element position,
int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],
int pos[CONST_MAZEX][CONST_MAZEY],int &tag);
bool Square1(int i,int j)
{
if(maze[i-1][j]&&maze[i][j-1]&&maze[i-1][j-1])
return true;
return false;
}
bool Square0(int i,int j)
{
if(!maze[i-1][j]&&!maze[i][j-1]&&!maze[i-1][j-1])
return true;
return false;
}
//----------------------------//
class element
{
public:
int row;
int col;
int dir;
};
bool EqualElement(const element a,const element b)
{
if(a.col==b.col&&a.row==b.row)
return true;
else
return false;
}
class offsets
{
public:
int vert;
int horiz;
public:
void GetValue(int vert=0,int horiz=0)
{
this->vert=vert;
this->horiz=horiz;
}
};
//---------------------------//
offsets move[4];
class Stack
{
private:
int top;
element error;
int count;
public:
element stack[MAX_STACK_SIZE];
public:
bool classmark[CONST_MAZEX][CONST_MAZEY];
int method; // method是对走法的处理,顺时针,逆时针等等
int sleep;
public:
Stack():top(-1)
{
this->InitMark();
}
void NewAntsCame()
{
this->InitMark();
this->classmark[1][1]=true;
::markall[1][1]++;
AddStack(1,1,1);
row=1;
col=1;
dir=1;
next_row=0;
next_col=0;
dir=0;
count=0;
pos[1][1]++;
//------------------
count++;
position=DeleteStack();
row=position.row;
col=position.col;
dir=position.dir;
::wild[1][1]=false;
sleep=0;
}
void SetDir(int dir)
{
this->dir=dir;
}
void StackEmpty()
{
top=-1;
}
element DeleteStack()
{
if(top==-1)
return error;
else
return stack[top--];
}
bool AddStack(const element & pa)
{
if(top==MAX_STACK_SIZE-1)
return false;
else
{
stack[++top]=pa;
return true;
}
}
bool AddStack(int row,int col,int dir)
{
if(top==MAX_STACK_SIZE-1)
return false;
else
{
top++;
stack[top].row=row;
stack[top].col=col;
stack[top].dir=dir;
return true;
}
}
bool Empty()
{
if(top==-1)
return true;
else
return false;
}
void InitMark();
//
private:
int row,col,next_row,next_col,dir;
element position;
public:
int SearchPathStepByStep(int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY]);
bool TestDir(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY]);
bool TestDirection(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY]);
bool HumanTestDirection(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY]);
int HumanSearchPathStepByStep(int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY]);
int SearchPathStepByStepRandom();
int GetLength()
{
return top;
}
element GetCurrentPosition()
{
element temp;
temp.row=row;
temp.col=col;
temp.dir=0 ; // temp.dir=dir;
return temp;
}
element GetLastPosition()
{
element temp;
temp.row=next_row;
temp.col=next_col;
temp.dir=0;
return temp;
}
~Stack()
{}
};
//--------------independent-------------------//
void Stack::InitMark()
{
int i,j;
for(i=0;i<CONST_MAZEX;i++)
for(j=0;j<CONST_MAZEY;j++)
{
if(maze[i][j]==1)
classmark[i][j]=true;
else
classmark[i][j]=false;
}
}
// 1 2 4 8
int Max(int x,int y ,int z,int w)
{
int max=x;
int count=0;
if(max<y)
max=y;
if(max<z)
max=z;
if(max<w)
max=w;
if(max==x)
count|=1;
if(max==y)
count|=2;
if(max==z)
count|=4;
if(max==w)
count|=8;
return count;
}
// if the direction is ok then i take it else i just be idle
//hxm begin
bool Stack::TestDir(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY])
//hxm end
{
this->dir=dir;
if(this->dir<0||this->dir>3)
return false;
next_row=row+move[dir].vert;
next_col=col+move[dir].horiz;
if(next_row==EXIT_ROW&&next_col==EXIT_COL)
{
found=true;
::mark[next_row][next_col]=true;
::markall[next_row][next_col]++;
::pos[next_row][next_col]++;
::pos[row][col]--;
::wild[next_row][next_col]=false;
position.row=row;
position.col=col;
position.dir=--dir;
AddStack(position);
row=next_row;
col=next_col;
dir=3;
AddStack(row,col,dir);
count++;
return true;
}
else
// 墙 在栈里 别人设的墙
if(!maze[next_row][next_col]&&!classmark[next_row][next_col]&&!mark[next_row][next_col])
{
classmark[next_row][next_col]=true;
::pos[row][col]--;
::pos[next_row][next_col]++;
::wild[next_row][next_col]=false;
position.row=row;
position.col=col;
position.dir=++this->dir;
AddStack(position);
row=next_row;
col=next_col;
this->dir=0;
count++;
return true;
}
return false;
}
bool Stack::TestDirection(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY])
{
this->dir=dir;
if(this->dir<0||this->dir>3)
return false;
int num=0;
while(num<4)
{
this->dir=this->dir%4;
next_row=row+move[this->dir].vert;
next_col=col+move[this->dir].horiz;
if(next_row==EXIT_ROW&&next_col==EXIT_COL)
{
found=true;
classmark[next_row][next_col]=true;
::wild[next_row][next_col]=false;
::pos[next_row][next_col]++;
::pos[row][col]--;
position.row=row;
position.col=col;
position.dir=--dir;
AddStack(position);
row=next_row;
col=next_col;
dir=3;
AddStack(row,col,dir);
count++;
return true;
}
else
if(!maze[next_row][next_col]&&!classmark[next_row][next_col]&&!mark[next_row][next_col])
{
classmark[next_row][next_col]=true;
::pos[row][col]--;
::pos[next_row][next_col]++;
::wild[next_row][next_col]=false;
position.row=row;
position.col=col;
position.dir=++this->dir;
AddStack(position);
row=next_row;
col=next_col;
this->dir=0;
count++;
return true;
}
else
{
++this->dir;
num++;
if(num==4)
{
::mark[row][col]=true;// tell others donot take this way;
count++;
::pos[row][col]--;
::markall[row][col]=UNACCESSABLE;
position=DeleteStack();
row=position.row;
col=position.col;
this->dir=position.dir;
::pos[row][col]++;
return false;
}
}
}
return false;
}
bool Stack::HumanTestDirection(int dir,int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY])
{
this->dir=dir;
if(this->dir<0||this->dir>3)
return false;
int num=0;
while(num<4)
{
this->dir=this->dir%4;
next_row=row+move[this->dir].vert;
next_col=col+move[this->dir].horiz;
if(next_row==EXIT_ROW&&next_col==EXIT_COL)
{
found=true;
classmark[next_row][next_col]=true;
::wild[next_row][next_col]=false;
::pos[next_row][next_col]++;
::pos[row][col]--;
position.row=row;
position.col=col;
position.dir=--dir;
AddStack(position);
row=next_row;
col=next_col;
dir=3;
AddStack(row,col,dir);
count++;
return true;
}
else
if(!maze[next_row][next_col]&&!classmark[next_row][next_col]&&!mark[next_row][next_col])
{
classmark[next_row][next_col]=true;
::pos[row][col]--;
::pos[next_row][next_col]++;
::wild[next_row][next_col]=false;
position.row=row;
position.col=col;
position.dir=++this->dir;
AddStack(position);
row=next_row;
col=next_col;
this->dir=0;
count++;
return true;
}
else
{
++this->dir;
num++;
Stack temp;
temp=*this;
if(num==4)
{
::mark[row][col]=true;// tell others donot take this way;
count++;
::pos[row][col]--;
::markall[row][col]=UNACCESSABLE;
position=DeleteStack();
row=position.row;
col=position.col;
this->dir=position.dir;
::pos[row][col]++;
int row1,col1,next_row1,next_col1;
element position1;
while(1)
{
int t=0;
position1=temp.DeleteStack();
row1=position1.row;
col1=position1.col;
temp.dir=position1.dir;
while(t<4)
{
temp.dir=temp.dir%4;
next_row1=row1+move[temp.dir].vert;
next_col1=col1+move[temp.dir].horiz;
if(!maze[next_row1][next_col1]&&!classmark[next_row1][next_col1]&&!mark[next_row1][next_col1])
{
return false;
}
temp.dir++;
t++;
}
::mark[row1][col1]=true;
::markall[row1][col1]=UNACCESSABLE;
}
return false;
}
}
}
return false;
}
int Stack::HumanSearchPathStepByStep(int markall[CONST_MAZEX][CONST_MAZEY],bool mark[CONST_MAZEX][CONST_MAZEY],int pos[CONST_MAZEX][CONST_MAZEY])
{
while(dir<4&&!found)
{
next_row=row+move[dir].vert;
next_col=col+move[dir].horiz;
if(next_row==EXIT_ROW&&next_col==EXIT_COL)
{
found=true;
classmark[next_row][next_col]=true;
::wild[next_row][next_col]=false;
::pos[next_row][next_col]++;
::pos[row][col]--;
position.row=row;
position.col=col;
position.dir=--dir;
AddStack(position);
row=next_row;
col=next_col;
dir=0;
AddStack(row,col,dir);
count++;
}
else
{
if(!maze[next_row][next_col]&&!classmark[next_row][next_col]&&!mark[next_row][next_col])
{
classmark[next_row][next_col]=true;
::pos[row][col]--;
::pos[next_row][next_col]++;
::wild[next_row][next_col]=false;
position.row=row;
position.col=col;
position.dir=++dir;
AddStack(position);
row=next_row;
col=next_col;
dir=0;
count++;
return 0;
}
else
{
++dir;
if(dir==4)
{
if(!Empty()&&!found)
{
Stack temp;
temp=*this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -