24,853
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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
#include "CMaze.h"
int main()
{
CMazePath maze;
maze.FindMazePath();
return 0;
}