#define SIZE 6/* MazeMap是迷宫的地图,数值1表示当前区域可行,数值0表示当前区域不可行, nDestRow表示目的行,nDestColumn表示目的列,在这里没有出栈之前,没 查询栈是否为空,是一个bug,毕竟不是所有的迷宫都有出路 迷宫的入口大致从坐标(1,1)开始,大致的走向如下: <3------>1 | | */void FindMazePath(int MazeMap[SIZE][SIZE],int nDestRow,int nDestColumn){ node* pVisit=(node*)malloc(100*sizeof(node)); memset(pVisit,0x00,100*sizeof(node)); //根节点进栈 入口坐标(1,1),排除零的干扰 int nBoundRow=SIZE-1; int nBoundColumn=SIZE-1; const int nEndRow=nDestRow; const int nEndColumn=nDestColumn; CStack stack; //初始化节点进栈的时候,最后的进栈方向应该是0,而不是1,因为它可以走四个方向,如果填写1,意味着不可能往3的方向走 stack.PushStack(1,1,1,0); StoreFoot(pVisit,1,1); int nRow,nColumn,nDirections; int nNowRow,nNowColumn; int nLastDirections; while(stack.IsStackNotEmpty()) { stack.PopStack(nRow,nColumn,nDirections,nLastDirections); cout<<"start to pop stack and check end:"< <<" "< <<" "< < nBoundColumn)||(3==nLastDirections)) { nDirections++; //越界,找下一个方向,或者不允许往回走 cout<<"choose another directions"< nBoundRow)||(4==nLastDirections)) { nDirections++; //越界,找下一个方向 } else { //判断当前的区域是否可通行,并且当前区域是没有走过的 if((0!=MazeMap[nNowRow][nNowColumn])&&(false==IsVisit(pVisit,nNowRow,nNowColumn))) { cout<<"start to visit"< <<"Push Stack:"< <<" "< < <<"Push Stack:"< <<" "< < #include #include using namespace std;#include "stack.h"CStack::CStack(){ m_pCStackTop=new SNode; if(NULL==m_pCStackTop) { exit(-1); } m_pCStackTop->pNextNode=NULL;}CStack::~CStack(){ SNode* pTmpNode=m_pCStackTop; while(NULL!=pTmpNode) { m_pCStackTop=pTmpNode->pNextNode; delete pTmpNode; pTmpNode=m_pCStackTop; }}int CStack::PushStack(int nRow, int nColumn, int nDirections,int nLastDirections){ SNode* pNewNode=new SNode; pNewNode->nColumn=nColumn; pNewNode->nRow=nRow; pNewNode->nDirections=nDirections; pNewNode->nLastDirections=nLastDirections; pNewNode->pNextNode=NULL; if(NULL==pNewNode) { exit(-1); } pNewNode->pNextNode=m_pCStackTop->pNextNode; m_pCStackTop->pNextNode=pNewNode; return 0;}int CStack::GetTop(int& nRow, int& nColumn, int& nDirections,int nLastDirections){ if(NULL==m_pCStackTop->pNextNode) { return -1; } nRow=m_pCStackTop->pNextNode->nRow; nColumn=m_pCStackTop->pNextNode->nColumn; nDirections=m_pCStackTop->pNextNode->nDirections; nLastDirections=m_pCStackTop->pNextNode->nLastDirections; return 0;}int CStack::PopStack(int &nRow, int &nColumn, int &nDirections, int nLastDirections){ if(NULL==m_pCStackTop->pNextNode) { return -1; } nRow=m_pCStackTop->pNextNode->nRow; nColumn=m_pCStackTop->pNextNode->nColumn; nDirections=m_pCStackTop->pNextNode->nDirections; nLastDirections=m_pCStackTop->pNextNode->nLastDirections; SNode* pTmpNode=m_pCStackTop->pNextNode; m_pCStackTop->pNextNode=pTmpNode->pNextNode; delete pTmpNode; return 0;}int CStack::DisplayStack(){ SNode* pTmpNode=m_pCStackTop->pNextNode; while((NULL!=pTmpNode)) { cout< nColumn< pNextNode; }}bool CStack::IsStackNotEmpty(){ if(NULL!=m_pCStackTop->pNextNode) { return true; } return false;}保存遍历的足迹:typedef struct node{ int nRow; int nColumn; int nUsed;}node;void StoreFoot(node* pVisit,int nRow,int nColumn){ node* pTmp=pVisit; for(int i=0;i<100;i++) { if(false==pTmp[i].nUsed) { pTmp[i].nColumn=nColumn; pTmp[i].nRow=nRow; pTmp[i].nUsed=true; return ; } } cout<<"can not find enough room to search not use"< <100;i++) { if(true==pTmp[i].nUsed) { if((nRow==pTmp[i].nRow)&&(nColumn==pTmp[i].nColumn)) { return true; } } } return false;}如下是简单构造的调用: int array[6][6]={0}; array[1][1]=1; array[1][3]=1; array[2][1]=1; array[2][2]=1; array[2][3]=1; array[2][4]=1; array[3][2]=1; array[4][2]=1; array[4][1]=1; array[4][3]=1; array[4][4]=1; array[5][4]=1; array[5][5]=1; FindMazePath(array,5,4);