作为新手琢磨了一天,终于想出走迷宫的暴力算法。
//写完以后走是走出来了,但这个代码实在是。。。。//怎么才能写出飞燕那样优雅的代码阿?大哥大姐们教教我啊。
#include <iostream>
#include <stdio.h>
using namespace std;
#define N 9
////////////////////////////////////////////////////////////////////////
// CStack can noly push 100 ints as max
//
class CStack // int stored only
{
private:
int nData[101]; // Storage
int nTop; // Pointer to top Storage
public:
CStack(); // constructor
int Push (int );
int Pop ();
int GetTop();
void Show ();// only for testing not use in prog.
};
CStack::CStack()
{
for (int i=0;i<101;i++) nData[i]=0;
nTop=0;
}
int CStack::Push(int data)
{
if (nTop==100) return 1;
nData[nTop++]=data;
return 0;
}
int CStack::Pop()
{
if (nTop==0) return 1;
nTop--;
return 0;
}
void CStack::Show() // Only for Testing
{
for (int i=0;i<=nTop;i++) printf("%d ",nData[i]+1);
printf ("\n");
}
int CStack::GetTop()
{
if (nTop==0) return nData[nTop];
return nData[nTop-1];
}
//////////////////////////////////////////////////////////////////
// Global variables
int nMap[N][N]={{1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1: wall
{1, 0, 0, 0, 1, 1, 1, 0, 1}, // 0: walk path
{1,-2, 1, 0, 0, 0, 0 ,0 ,1}, // -1: start point
{1, 1, 1, 0, 1, 1, 1, 1, 1}, // -2: end point
{1, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0,-1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}};
int nPast[N][N];// to save the path already walked
int sx=0,sy=0,ex=0,ey=0,cx=0,cy=0;// start x y, end x y,current x y
CStack *stkx=new CStack;// x position stack
CStack *stky=new CStack;// y position stack
/////////////////////////////////////////////
// Functions
int MapIni()
{
for (int i=0;i<N;i++)
for (int j=0;j<N;j++)
{
if (nMap[i][j]==-1) {sx=j;sy=i;}
if (nMap[i][j]==-2) {ex=j;ey=i;}
nPast[i][j]=nMap[i][j];
}
nPast[sy][sx]=2;// Start point is waled.
cx=sx;// save current position as start point
cy=sy;
return 0;
}
int FindPath()
{
stkx->Push(sx);
stky->Push(sy);// Push start point
while(nMap[cy][cx]!=-2)
{
// reset the current xy if already poped
cy=stky->GetTop();cx=stkx->GetTop();
// if next position can be walked and never walked before
if ((nMap[cy][cx+1]<1) && (nPast[cy][cx+1]!=2))
{
cx++;// Go right
stkx->Push(cx);stky->Push(cy);//Push x y
nPast[cy][cx]=2;// here already walked
}
else if ((nMap[cy-1][cx]<1) && (nPast[cy-1][cx]!=2))
{// Go up
cy--;
stkx->Push(cx);stky->Push(cy);
nPast[cy][cx]=2;
}
else if ((nMap[cy+1][cx]<1) && (nPast[cy+1][cx]!=2))
{// Go down
cy++;
stkx->Push(cx);stky->Push(cy);
nPast[cy][cx]=2;
}
else if ((nMap[cy][cx-1]<1) && (nPast[cy][cx-1]!=2))
{// Go left
cx--;
stkx->Push(cx);stky->Push(cy);
nPast[cy][cx]=2;
}
else {stkx->Pop();stky->Pop();}// no way to go: pop
}
return 0;
}
void ShowPath()
{
int t=0;
while(!t)
{
nMap[stky->GetTop()][stkx->GetTop()]=8;
stkx->Pop();
t=stky->Pop();
}
nMap[sy][sx]=-1;
nMap[ey][ex]=-2;
for (int i=0;i<N;i++)
{ for (int j=0;j<N;j++)
{
if (nMap[i][j]==0 ) printf (" ");
else if (nMap[i][j]==8 ) printf (".");
else if (nMap[i][j]==-1) printf ("S");
else if (nMap[i][j]==-2) printf ("E");
else /*(nMap[i][j]==1 )*/ printf ("X");
}
printf("\n");
}
}
void ShowMap()
{
for (int i=0;i<N;i++)
{ for (int j=0;j<N;j++)
{
if (nMap[i][j]==0 ) printf (" ");
else if (nMap[i][j]==8 ) printf (".");
else if (nMap[i][j]==-1) printf ("S");
else if (nMap[i][j]==-2) printf ("E");
else /*(nMap[i][j]==1 )*/ printf ("X");
}
printf("\n");
}
}
////////////////////////////////////
// Main
//
int main(int argc, char *argv[])
{
MapIni();
ShowMap();
FindPath();
ShowPath();
system ("pause");
delete stkx;
delete stky;
return 0;
}
//另外,看了飞燕的论坛里面说寻路经的时候不用每个方向判断,用个什么DX就能处理了,我看不懂
//能不能请哪位大哥具体说明一下。