Coordinate*temp[4];//存放有效相邻格子的数组
if(hasPath(currentX-1,currentY))//up
{
i=0;
temp[i]=&mazePointer[currentX-1][currentY];
}
if(hasPath(currentX,currentY-1))//left
{
i=1;
temp[i]=&mazePointer[currentX][currentY-1];
}
if(hasPath(currentX,currentY+1))//right
{
i=2;
temp[i]=&mazePointer[currentX][currentY+1];
}
if(hasPath(currentX+1,currentY))//down
{
i=3;
temp[i]=&mazePointer[currentX+1][currentY];
}
if(accesseCount==0)
count2=i;
else
{
intt;
t=count2;
count2=i;
count1=t;
}
if(i!
=-1)
{
currentCoordinate->accessed=true;
pathStack.push(*currentCoordinate);//将上一个格子坐标进栈
currentCoordinate=temp[i];
currentCoordinate->value=2;
currentCoordinate->accessed=true;
accesseCount++;
currentX=currentCoordinate->x;
currentY=currentCoordinate->y;
}
else
{
if(!
pathStack.empty())//当前格子无可走的相邻格子,返回到上一个格子
{
mazePointer[currentX][currentY].value=-1;
currentCoordinate=&pathStack.top();
pathStack.pop();
}
}
}
}
4.BFS算法实现
voidBFS:
:
mazePath()
{
currentCoordinate=newCoordinateBFS(0,start);
intcount1=0,count2=0;//count1为记录前一个被访问格子(栈顶部格子)可走相邻格子数,count2为当前访问格子的可走相邻格子数
intcurrentX=currentCoordinate->x;
intcurrentY=currentCoordinate->y;
mazePointer[currentX][currentY].accessed=true;
mazePointer[currentX][currentY].value=2;
//intentry=startPoint;//入口id
//intexit=endPoint;//出口id
//记录所有房间的访问路径,traPath[i]表示访问到房间i的路径上的前一节点
//vectortraPath(endPoint,-1);
//depathQuerypathQuery;
s_i=0;
s=newCString[m*n];
pathQuery.push_back(mazePointer[0][start]);
while(!
pathQuery.empty())
{
if(currentX==(m-1))
break;
inti=0;
//if(mazePointer[currentX][currentY].value==2)//如果已经该房间已经访问过了,则跳过
//continue;
if(hasPath(currentX,currentY-1))
{
pathQuery.push_back(mazePointer[currentX][currentY-1]);
//mazePointer[currentX][currentY-1].value=2;
currentCoordinate->accessed=true;
i++;
}
if(hasPath(currentX,currentY+1))
{
pathQuery.push_back(mazePointer[currentX][currentY+1]);
//mazePointer[currentX][currentY+1].value=2;
currentCoordinate->accessed=true;
i++;
}
if(hasPath(currentX-1,currentY))
{
pathQuery.push_back(mazePointer[currentX-1][currentY]);
//mazePointer[currentX-1][currentY].value=2;
currentCoordinate->accessed=true;
i++;
}
if(hasPath(currentX+1,currentY))
{
pathQuery.push_back(mazePointer[currentX+1][currentY]);
//mazePointer[currentX+1][currentY].value=2;
currentCoordinate->accessed=true;
i++;
}
if(i==0)
{
mazePointer[currentX][currentY].value=-1;
}
else
{
mazePointer[currentX][currentY].value=2;
}
pathQuery.pop_front();
currentCoordinate=&pathQuery.front();
currentX=currentCoordinate->x;
currentY=currentCoordinate->y;
s[s_i++].Format(_T("(%d,%d)\n"),currentX,currentY);
//cout<<"X:
"<"<}
}
5.MFC按钮功能实现
voidCMaze_MFCDlg:
:
OnBnClickedCreatemaze()
{
//TODO:
在此添加控件通知处理程序代码
intm,n;
UpdateData(TRUE);
if(m_MazeX==0||m_MazeY==0)
{
m=50;
n=50;
}
else
{
m=(int)m_MazeX;
n=(int)m_MazeY;
}
Mazemaze(m,n);//创建迷宫对象
maze.createMaze();
r=maze.getGraphRow();
c=maze.getGraphColumn();
Arr=maze.getGraph();
Arr=newint*[maze.getGraphRow()];
for(inti=0;iArr[i]=newint[maze.getGraphColumn()];
for(inti=0;ifor(intj=0;jArr[i][j]=maze.getGraph()[i][j];
//doublelength=50;
//doublewide=50;
/*intx=5;inty=5;*/
HDChdc=:
:
GetDC(m_hWnd);
HBRUSHhbrush_BK,hbrushOld_BK;
hbrush_BK=CreateSolidBrush(RGB(255,255,255));
hbrushOld_BK=(HBRUSH)SelectObject(hdc,hbrush_BK);
:
:
Rectangle(hdc,0,0,700,700);
SelectObject(hdc,hbrushOld_BK);
DeleteObject(hbrush_BK);
for(inti=0;i{
for(intj=0;j{
if(Arr[i][j]==1)//墙
{
HBRUSHhbrush,hbrushOld;
hbrush=CreateSolidBrush(RGB(138,54,15));
hbrushOld=(HBRUSH)SelectObject(hdc,hbrush);
:
:
Rectangle(hdc,i*10,j*10,i*10+10,j*10+10);
SelectObject(hdc,hbrushOld);
DeleteObject(hbrush);
}
}
}
:
:
ReleaseDC(m_hWnd,hdc);
}
voidCMaze_MFCDlg:
:
OnBnClickedDfs()
{
//TODO:
在此添加控件通知处理程序代码
DFSdfs(Arr,r,c);
dfs.mazePath();
/*dfs.printPath();*/
HDChdc=:
:
GetDC(m_hWnd);
for(inti=0;i{
for(intj=0;j{
if(dfs.getValue(i,j)==2)
{
HBRUSHhbrush,hbrushOld;
hbrush=CreateSolidBrush(RGB(0,0,255));
hbrushOld=(HBRUSH)SelectObject(hdc,hbrush);
:
:
Rectangle(hdc,i*10,j*10,i*10+10,j*10+10);
SelectObject(hdc,hbrushOld);