📄 maze.h
字号:
::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;
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=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 0;
}
t++;
}
::mark[row1][col1]=true;
::markall[row1][col1]=UNACCESSABLE;
}
return 0;
}
else
return -1;
}
}
}
}
if(found)
{
return count;
}
else
{
return 0;
}
}
int Stack::SearchPathStepByStep(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)
{
::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;
dir=position.dir;
::pos[row][col]++;
return 0;
}
else
return -1;
}
}
}
}
if(found)
{
return count;
}
else
{
return 0;
}
}
int Stack::SearchPathStepByStepRandom()
{
int num=0;
int method=rand()%2;
dir=rand()%4;
while(!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;
::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])
{
classmark[next_row][next_col]=true;
::pos[row][col]--;
::pos[next_row][next_col]++;
position.row=row;
position.col=col;
position.dir=++dir;
AddStack(position);
row=next_row;
col=next_col;
dir=0;
count++;
return 0;
}
else
{
++num;
if(num==4)
{
if(!Empty()&&!found)
{
count++;
::pos[row][col]--;
position=DeleteStack();
row=position.row;
col=position.col;
dir=position.dir;
::pos[row][col]++;
return 0;
}
else
return -1;
}
}
}
if(method==0)
dir=(++dir)%4;
if(method==1)
dir=((--dir)+4)%4;
}
if(found)
{
return count;
}
else
{
return 0;
}
}
void SimpleMaze()
{
int i=1,j=1;
maze[i][j]=0;
int k;
while(i!=EXIT_ROW||j!=EXIT_COL)
{
if(i==EXIT_ROW)
{
while(j!=EXIT_COL)
maze[i][++j]=0;
break;
}
if(j==EXIT_COL)
{
while(i!=EXIT_ROW)
maze[++i][j]=0;
break;
}
k=rand()%2;
if(k==0)
{
i++;
maze[i][j]=0;
continue;
}
if(k==1)
{
j++;
maze[i][j]=0;
continue;
}
}
}
void GenerateMaze()
{
int i,j;
int i1,j1;
for(i=0;i<SIZEX_MAZE;i++)
for(j=0;j<SIZEY_MAZE;j++)
maze[i][j]=1;
SimpleMaze();
for(i=2;i<SIZEX_MAZE-2;i++)
for(j=2;j<SIZEY_MAZE-2;j++)
{
if(Square1(i,j))
{
i1=i-rand()%2;
j1=j-rand()%2;
maze[i1][j1]=0;
}
}
Stack _stack;
if(SearchPath(_stack)==0)
{
SimpleMaze();
}
}
void ShowMaze()
{
int i=0,j=0;
for(i=1;i<SIZEX_MAZE-1;i++)
{
for(j=1;j<SIZEY_MAZE-1;j++)
cout<<maze[i][j]<<" ";
cout<<endl;
}
}
void InitMark()
{
int i,j;
for(i=0;i<CONST_MAZEX;i++)
for(j=0;j<CONST_MAZEY;j++)
{
wild[i][j]=true;
mark[i][j]=false;
if(maze[i][j]==1)
markall[i][j]=UNACCESSABLE;
else
markall[i][j]=0;
pos[i][j]=0;
}
move[0].GetValue(-1,0);
move[1].GetValue(0,1);
move[2].GetValue(1,0);
move[3].GetValue(0,-1);
}
long SearchPath(Stack & _stack)
{
int row,col,next_row,next_col,dir;
long count=0;
bool found=false;
element position;
InitMark();
mark[1][1]=true;
_stack.AddStack(1,1,1);
while(!_stack.Empty()&&!found)
{
position=_stack.DeleteStack();
row=position.row;
col=position.col;
dir=position.dir;
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;
else
if(!maze[next_row][next_col]&&!mark[next_row][next_col])
{
mark[next_row][next_col]=1;
position.row=row;
position.col=col;
position.dir=++dir;
_stack.AddStack(position);
row=next_row;
col=next_col;
dir=0;
count++;
}
else
++dir;
}
}
if(found)
{
return count;
}
else
return 0;
}
void InitAll()
{
GenerateMaze();
InitMark();
}
int Min(int x,int y ,int z,int w,bool xi,bool yi,bool zi,bool wi)
{
int m=x*y*z*w;
int count;
if(m==0)
{
count=0;
if(x==0&&xi)
count+=1;
if(y==0&&yi)
count+=2;
if(z==0&&zi)
count+=4;
if(w==0&&wi)
count+=8;
return count;
}
return 0;
// return 0 shows that it should do as priority and not the chance
//如果这个时候有两个路口有人,一个路口没有人进去过,那么全塞到这个路口里面去
}
// hxm begin
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)
// hxm end
{
int max,i;
int num;
int dir;
int x=position.row;
int y=position.col;
int turn;
switch(count)
{
case 1: // all go to (x-1,y)
for(i=0;i<n;i++)
{
if(EqualElement(_stackarray[i].GetCurrentPosition(),position))
{
dir=0;
if(tag==4)
{
_stackarray[i].TestDirection(dir,markall,mark,pos);
tag=0;
return ;
}
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
max=markall[x+1][y];
if(max<markall[x][y-1])
{ max=markall[x][y-1];}
if(max<markall[x][y+1])
{ max=markall[x][y+1];}
num=0;
if(max==markall[x+1][y]) num+=2;
if(max==markall[x][y-1]) num+=4;
if(max==markall[x][y+1]) num+=8;
tag++;
SubAllocate(num,_stackarray,n,position,markall,mark,pos,tag);
}
}
}
break;
case 2: // all go to (x+1,y)
for(i=0;i<n;i++)
{
if(EqualElement(_stackarray[i].GetCurrentPosition(),position))
{
dir=2;
if(tag==4)
{
_stackarray[i].TestDirection(dir,markall,mark,pos);
tag=0;
return ;
}
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
max=markall[x-1][y];
if(max<markall[x][y-1])
{ max=markall[x][y-1];}
if(max<markall[x][y+1])
{ max=markall[x][y+1];}
num=0;
if(max==markall[x-1][y]) num+=1;
if(max==markall[x][y-1]) num+=4;
if(max==markall[x][y+1]) num+=8;
tag++;
SubAllocate(num,_stackarray,n,position,markall,mark,pos,tag);
}
}
}
break;
case 3: // all to (x-1,y) and (x+1,y)
turn=0;
for(i=0;i<n;i++)
{
if(EqualElement(_stackarray[i].GetCurrentPosition(),position))
{
if(turn==0)
{
dir=0;
if(tag==4)
{
_stackarray[i].TestDirection(dir,markall,mark,pos);
tag=0;
return ;
}
turn=1;
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
dir=2;
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
turn=0;
max=markall[x][y-1];
if(max<markall[x][y+1])
{ max=markall[x][y+1];}
num=0;
if(max==markall[x][y-1]) num+=4;
if(max==markall[x][y+1]) num+=8;
tag++;
SubAllocate(num,_stackarray,n,position,markall,mark,pos,tag);
}
}
}
else
{
dir=2;
if(tag==4)
{
_stackarray[i].TestDirection(dir,markall,mark,pos);
tag=0;
return ;
}
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
dir=0;
turn=0;
if(_stackarray[i].TestDir(dir,markall,mark,pos)==false)
{
turn=1;
max=markall[x][y-1];
if(max<markall[x][y+1])
{ max=markall[x][y+1] ;}
num=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -