⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 microbeway.h

📁 maze目录下包含了4个迷宫相关的源代码:简单的迷宫生成算法、复杂的迷宫生成算法、简单的迷宫搜索算法、复杂的迷宫搜索算法
💻 H
📖 第 1 页 / 共 2 页
字号:
		{
			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;
			}
		}

	MStack _MStack;
	if(MSearchPath(_MStack)==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 MInitMark()
{
	int i,j;
	for(i=0;i<SIZEX_MAZE;i++)
		for(j=0;j<SIZEY_MAZE;j++)
		{
			mark[i][j]=false;
			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 MSearchPath(MStack & _MStack)
{
	int row,col,next_row,next_col,dir;
	long count=0;
	bool found=false;
	element position;
	MInitMark();
	mark[1][1]=true;
	_MStack.AddMStack(1,1,1);
	while(!_MStack.Empty()&&!found)
	{
		position=_MStack.DeleteMStack();
		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;
					_MStack.AddMStack(position);
					row=next_row;
					col=next_col;
					dir=0;
					count++;

				}
				else
					++dir;
		}
	}
	if(found)
	{
		return count;
	}
	else
		return 0;
}


void MInitAll()
{
	GenerateMaze();
	MInitMark();
}

int MMin(int x,int y ,int z,int w)
{
	int m=x*y*z*w;
	int count;
	if(m==0)
	{
		count=0;
		if(x==0)
			count+=1;
		if(y==0)
			count+=2;
		if(z==0)
			count+=4;
		if(w==0)
			count+=8;
		return count;
	}
	return 0;
}

void MAllocate(MStack _MStackarray[],int n)
{
	int x,y;
	int count=0;
	int i=0;
	int dir=0;
	element position;
	int turn=0;
//	int length=0;
	int countnum=0;
	bool once=true;

	int pos[CONST_MAZEX][CONST_MAZEY];		// record all objects position at the same time
	MStack newMStack[CONST_ANT_NUM];


	::step++;
	for(i=0;i<n;i++)
		newMStack[i]=_MStackarray[i];

	for(i=0;i<SIZEX_MAZE;i++)
		for(int j=0;j<SIZEY_MAZE;j++)
		{
			pos[i][j]=::pos[i][j];
		}


	for(x=1;x<SIZEX_MAZE-1;x++)
		for(y=1;y<SIZEY_MAZE-1;y++)
		{
			if(pos[x][y]<=0)
				continue;
			else
			{
				position.row=x;
				position.col=y;
				position.dir=1;
				
				count=MMin(maze[x-1][y],maze[x+1][y],maze[x][y-1],maze[x][y+1]);
				
					switch(count)
					{
					case 0: cout<<"false "<<endl;break;
					case 1:
					case 2:
					case 4:
					case 8:
						{
							for(i=0;i<n;i++)
							{	
								if(EqualElement(_MStackarray[i].GetCurrentPosition(),position))
								{		
									if(count==1)
										dir=0;   //all go to (x-1,y)
									else if(count==2)
										dir=2;	// all go to(x+1,y)
									else if(count==4)
										dir=3;	// all to to (x,y-1)
									else if(count==8)
										dir=1;	// all go to (x,y+1)
								//	newMStack[i].SetDir(dir);
									newMStack[i].TestDirection(dir);
						//				//if((++countnum)==n)	return;
								}
							}
							break;
						}
					case 3:	
					case 5:
					case 6:
						{
								turn=0;
								for(i=0;i<n;i++)
								{	
									if(EqualElement(_MStackarray[i].GetCurrentPosition(),position))
									{
										if(turn==0)
										{
											if(count== 3 || count ==5)
												dir=0;
											else if(count==6)		// all go to (x+1,y) and (x,y-1)
												dir=2;
											newMStack[i].TestDirection(dir);
											turn=1;
										}
										else
										{
											if(count==3)
												dir=2;		// all to (x-1,y) and (x+1,y)
											else if(count==5 || count==6)
												dir=3;		// all go to (x-1,y) and (x,y-1)
											newMStack[i].TestDirection(dir);
											turn=0;
										}
									}
								}
								break;
						}
					case 7:
					case 11:
					case 13:
					case 14:
					{
// 7:	all go to (x-1,y) and (x+1,y) and (x,y-1)
// 11:  all go to (x-1,y) and (x+1,y) and (x,y+1)
// 13:	all go to (x-1,y) and (x,y-1) and (x,y+1)
// 14:	all go to (x+1,y) and (x,y-1) and (x,y+1)
								turn=0;
								for(i=0;i<n;i++)
								{	
									if(EqualElement(_MStackarray[i].GetCurrentPosition(),position))
									{
										switch(turn)
										{
										case 0:
											if(count==7 || count ==11 || count==13)
												dir=0;
											else
												dir=2;
											newMStack[i].TestDirection(dir);
											turn=1;
											break;
										case 1:
											if(count==7 || count==11)
												dir=2;
											else 
												dir=3;
											newMStack[i].TestDirection(dir);
											turn=2;
											break;
										case 2:
											if(count==7)
												dir=3;
											else
												dir=1;
											newMStack[i].TestDirection(dir);
											turn=0;
											break;
										}
									}
								}
								break;
							}
					
						case 9:			// all go to (x-1,y) and (x,y+1)
						case 10:		// all go to (x+1,y) and (x,y+1)
						case 12:		// all go to (x,y-1) and (x,y+1)
							{
								turn=0;
								for(i=0;i<n;i++)
								{	
									if(EqualElement(_MStackarray[i].GetCurrentPosition(),position))
									{
										switch(turn)
										{
										case 0:
											if(count==9)			
												dir=0;
											else if(count==10)	
												dir=2;
											else if(count==12)	
												dir=3;
												newMStack[i].TestDirection(dir);
												turn=1;
											break;
										case 1:
											dir=1;
											newMStack[i].TestDirection(dir);
											turn=0;
											break;
										}
									}
								}
								break;
							}
						case 15:
							{
								turn=0;
								for(i=0;i<n;i++)
								{	
									if(EqualElement(_MStackarray[i].GetCurrentPosition(),position))
									{
										switch(turn)
										{
										case 0:
											dir=0;
											newMStack[i].TestDirection(dir);
											turn=1;
											break;
										case 1:
											dir=1;
											newMStack[i].TestDirection(dir);
											turn=2;
											break;
										case 2:
											dir=2;
											newMStack[i].TestDirection(dir);
											turn=3;
											break;
										case 3:
											dir=3;
											newMStack[i].TestDirection(dir);
											turn=3;
											break;
										}
									}
								}
								break;
							}
					default:
							break;
			
					}
				}
		}
	for(i=0;i<n;i++)
		_MStackarray[i]=newMStack[i];

}

/*
void main()
{
	MMinitAll();
	ShowMaze();
	int i;

	cout<<endl<<" this is the final result "<<endl;
	//-----------------final test--------------------------------------//
	MMinitMark();
	MStack _MStackarray[MAX_ANT_NUM];
	
	for(i=0;i<MAX_ANT_NUM;i++)
		_MStackarray[i].NewAntsCame ();
	found=false;
	int tag=0;
	element position;
	bool x; 
	element temp;
	int j;
	while(1)
	{
		MAllocate(_MStackarray,MAX_ANT_NUM);
		for(i=0;i<MAX_ANT_NUM;i++)
		{
			for(j=0;j<_MStackarray[i].GetLength();j++)
			{
				temp=_MStackarray[i].MStack[j];
			}
		}
		cout<<endl;
		for(i=1;i<SIZEX_MAZE-1;i++)
		{
			for(j=1;j<SIZEY_MAZE-1;j++)
				cout<<pos[i][j]<<"  ";
			cout<<"      ";
			for(j=1;j<SIZEY_MAZE-1;j++)
				cout<<maze[i][j]<<"  ";

			cout<<endl;
		}
		
		for(i=0;i<MAX_ANT_NUM;i++)
		{	
			position=_MStackarray[i].GetCurrentPosition();
			if(position.row==EXIT_ROW&&position.col==EXIT_COL)
			{
				tag=i;
				break;
			}
			x=found;
		}

		if(tag)
			break;
		if(found==true)
			break;
	}
	cout<<endl;
	cout<<"------------------final test------------------"<<endl;
	for(i=0;i<_MStackarray[tag].GetLength()+1;i++)
	{
		cout<<_MStackarray[tag].MStack[i].row<<"--"<<_MStackarray[tag].MStack[i].col<<";";
	}
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -