C++程序的走迷宫问题

tianchuanleo 2008-06-16 12:07:11
要非递归 能够显示生成的迷宫矩阵
...全文
325 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
HNLGDXYJS 2008-06-16
  • 打赏
  • 举报
回复
bccn的一个帖子,你可以参考一下,有递归,非递归.还有强人写的一个很漂亮的代码.

http://bbs.bccn.net/viewthread.php?tid=212690&highlight=%2Bzjl138
simo110 2008-06-16
  • 打赏
  • 举报
回复
考试的时候随手写了个
输出结果为 入口,出口,原始的迷宫,经过路径,演示迷宫


=============CMaze.cpp==================



#include "CMaze.h"
using namespace std;

CMaze::CMaze()
{

}


CMaze::CMaze(int iOrder, CPoint stSeat, int iDi):
m_iOrder(iOrder),
m_stSeat(stSeat),
m_iDi(iDi)
{

}

CMaze & CMaze::operator = (const CMaze & ccOther)
{
m_iOrder = ccOther.m_iOrder;
memcpy(&m_stSeat, &(ccOther.m_stSeat), sizeof(CPoint));
m_iDi = ccOther.m_iDi;

return *this;
}

CMazePath::CMazePath()
{
m_stStart.x = 1;
m_stStart.y = 1;

m_stEnd.x = 8;
m_stEnd.y = 8;

m_stCur = m_stStart;



for (int iCoutI = 0; iCoutI < 10; iCoutI++)
for (int iCoutJ = 0; iCoutJ < 10; iCoutJ++)
{
cMazeMatrix[iCoutI][iCoutJ] = 0;

cMazeMatrix[iCoutI][0] = 1;
cMazeMatrix[0][iCoutJ] = 1;
cMazeMatrix[iCoutI][9] = 1;
cMazeMatrix[9][iCoutJ] = 1;


}

cMazeMatrix[1][3] = 1;
cMazeMatrix[1][7] = 1;

cMazeMatrix[2][3] = 1;
cMazeMatrix[2][7] = 1;

cMazeMatrix[3][5] = 1;
cMazeMatrix[3][6] = 1;

for (int iCoutJ = 2; iCoutJ < 5; iCoutJ++)
{
cMazeMatrix[4][iCoutJ] = 1;
}

cMazeMatrix[5][4] = 1;

cMazeMatrix[6][2] = 1;
cMazeMatrix[6][6] = 1;

for (iCoutJ = 2; iCoutJ < 8; iCoutJ++)
{
if (5 != iCoutJ)
{
cMazeMatrix[7][iCoutJ] = 1;
}
}

cMazeMatrix[8][1] = 1;


cout << m_stStart.x << " " << m_stStart.y << endl;
cout << m_stEnd.x << " " << m_stEnd.y << endl;
for ( iCoutI = 0; iCoutI < 10; iCoutI++)
{
for ( iCoutJ = 0; iCoutJ < 10; iCoutJ++)
{
cout << cMazeMatrix[iCoutI][iCoutJ] <<" ";
}
cout << endl;
}


}

bool CMazePath::IsPass(CPoint & cpCur)
{
return (cMazeMatrix[cpCur.y][cpCur.x] == 0) ? true: false;
}

void CMazePath::FootPrint(CPoint & cpCur)
{
cout << cpCur.y << " " << cpCur.x << endl;
cMazeMatrix[cpCur.y][cpCur.x] += 'o'; //坐标和行列相反
}

CPoint & CMazePath::NextPos(CPoint &cpCur, int iWay)
{
switch(iWay)
{
case 1 :
cpCur.x++;
break;

case 2 :
cpCur.y++;
break;

case 3 :
cpCur.x--;
break;

case 4 :
cpCur.y--;
break;

default:
break;
}
return cpCur;
}


void CMazePath::MaketPrint(CPoint & cpCur)
{
cMazeMatrix[cpCur.y][cpCur.x] = 4;
return;
}


bool CMazePath::FindMazePath()
{
m_stCur = m_stStart;
int iCurStep = 1;
do
{
if (IsPass(m_stCur))//当前位置是能通过的
{
FootPrint(m_stCur);
staMaze.push(CMaze(iCurStep, m_stCur, 1));
if ( !memcmp(&m_stCur, &m_stEnd, sizeof(CPoint)) )
{

for (int iCoutI = 0; iCoutI < 10; iCoutI++)
{
for (int iCoutJ = 0; iCoutJ < 10; iCoutJ++)
{
cout << cMazeMatrix[iCoutI][iCoutJ] <<" ";
}
cout << endl;
}


return true;
}
m_stCur = NextPos(m_stCur, 1);
iCurStep++;
}
else
{
if (!staMaze.empty())
{
CMaze CurMaze;

CurMaze = staMaze.top();
staMaze.pop();
while ( (4 == CurMaze.m_iDi)&& !(staMaze.empty()) )
{
MaketPrint(CurMaze.m_stSeat);
CurMaze = staMaze.top();
staMaze.pop();
}
if (4 > CurMaze.m_iDi)
{
CurMaze.m_iDi++;
staMaze.push(CurMaze);
m_stCur = NextPos(CurMaze.m_stSeat, CurMaze.m_iDi);
}

}

}
} while ( !staMaze.empty() );


return false;

}



==============================CMaze.h================================

#ifndef __CMAZE_H__
#define __CMAZE_H__

#include <iostream>
#include <stack>
#include <cstdlib>

struct CPoint
{
int x;
int y;
};

class CMazePath;

class CMaze
{
public:
CMaze(int, CPoint, int );
CMaze();
CMaze & operator = (const CMaze & ccOther);

friend class CMazePath;


protected:
private:
int m_iOrder;//当前序列号
CPoint m_stSeat;//当前坐标
int m_iDi;//行进方向
};

class CMazePath
{
public:
CMazePath();
bool FindMazePath();

private:

bool IsPass(CPoint &);//当前位置可以通过
CPoint & NextPos(CPoint &, int);//当前的下一个位置,第二个参数来控制当前位置的什么方向
void MaketPrint(CPoint &);
void FootPrint(CPoint &);

CPoint m_stStart, m_stEnd, m_stCur;
std::stack<CMaze> staMaze;
char cMazeMatrix[10][10];
};

#endif


=================================mainProcess.cpp=============================

#include "CMaze.h"

int main()
{
CMazePath maze;
maze.FindMazePath();

return 0;
}

24,853

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧