資源簡介
數據結構課程設計之C++編寫的迷宮問題路徑求解程序,使用的是棧方法,即將路徑上每一步存在棧中,迷宮文件格式見程序提示,壓縮包內已經給出了三個測試用的迷宮地圖可用來測試,支持分步顯示查找路徑過程功能,當給出的迷宮是死迷宮時會提示用戶。注:壓縮包是個DevC++項目,如需VC編譯請把.cpp和.h文件提取出來建立項目。

代碼片段和文件信息
//迷宮問題
//作者:金航161530115
//完成日期:2016-12-23
#include?
#include?
#include?
#include?
#include?
#include?
#include?“MazeStack.h“
#define?MAXWIDTH?50
using?namespace?std;
int?maze[MAXWIDTH][MAXWIDTH];
MazeStack?stk;
struct?tagMazeinfo
{
int?m; //行數
int?n; //列數
int?x1?y1; //起點坐標
int?x2?y2;?//終點坐標?
}?MI;
bool?StepShow;
void?LoadMaze(const?char?filename[]);
void?PrintMaze(bool?StepShow);
bool?IsThisStepAva(Node?&pt?Node?lastpt?const?int?cmd);
#define?ASKAVA?1
#define?GOINTO?2
void?Pause(const?char?*say);
bool?JudgeDest(Node?&curpt);
int?main()
{
char?filename[100];
Node?curpt?lastpt;
bool?flag?=?false;
cout?<“迷宮問題\n“
?<“迷宮存在文件中,文件格式如下:\n“
?<“第一行為行數m?列數n(含最外層邊界)\n“
?<“現約定以整個文件第四行第一個數為坐標(0?0),向右為x正方向,向下為y正方向\n“
?<“第二行為起點坐標y1?x1\n“
?<“第三行為終點坐標y2?x2\n“
?<“接下來給出m行n列的地圖\n“
?<“地圖中數字的代表說明\n“
?<“0-可步入的坐標\n1-障礙物\n2-探索路徑\n3-當前位置\n“
?<“數據限制:\n3<=m<=50?3<=n<=50?1<=y1<=m-2?1<=x1<=n-2?1<=y2<=m-2?1<=x2<=n-2\n“
?<“y1-y2+x1-x2!=0?array[y1][x1]!=1?array[y2][x2]!=1\n“
?<“請輸入文件名:\n>“;
cin?>>?filename;
LoadMaze(filename);
if?(ERROR?==?stk.InitStack())
{
cout?<“棧初始化失敗?!?
Pause(“按任意鍵退出.?.?.“);
exit(1);
}
curpt.horPos?=?1;
curpt.verPos?=?1;
curpt.under?=?NULL;
maze[curpt.horPos][curpt.verPos]?=?CURPOS;
if?(StepShow)
{
stk.ShowAllStack();
}
PrintMaze(true);
Pause(NULL);
cout?<“\n\n\n“;
while?(true)
{
GOAHEADLOOP:
flag?=?false;
for?(curpt.heading?=?RIGHT;
?curpt.heading?<=?UP;
?curpt.heading++)
{
if?(IsThisStepAva(curpt?lastpt?ASKAVA))
{
stk.GetTop(lastpt);
if?(curpt.heading?==?(lastpt.heading?+?2)
||?curpt.heading?==?(lastpt.heading?-?2))
{
continue;
}
IsThisStepAva(curpt?lastpt?GOINTO);
PrintMaze(StepShow);
if?(JudgeDest(curpt))
{
goto?PROGRAMEND;
}
Pause(NULL);
// cout?<“\n\n\n“;
flag?=?true;
break;
}
}//End?of?GOAHEADLOOP
GOBACKLOOP:
if?(!flag?&&?!stk.IsStackEmpty())
{
while?(true)
{
stk.NodeCopy(lastpt?curpt);
maze[curpt.horPos][curpt.verPos]?=?UNSTEPED;
stk.Pop(curpt);
maze[curpt.horPos][curpt.verPos]?=?CURPOS;
if?(StepShow)
{
stk.ShowAllStack();
}
PrintMaze(StepShow);
if?(stk.IsStackEmpty()
&&?curpt.horPos?==?MI.y1
&&?curpt.verPos?==?MI.x1
&&?curpt.heading?>=?UP)? ?
{
cout?<“此迷宮無解。\n“;
goto?PROGRAMEND;
}
Pause(NULL);
// cout?<“\n\n\n“;
for?(curpt.heading?=?curpt.heading;
?curpt.heading?<=?UP;
?curpt.heading++)
{
if?(IsThisStepAva(curpt?lastpt?ASKAVA))
{
IsThisStepAva(curpt?lastpt?GOINTO);
PrintMaze(StepShow);
if?(JudgeDest(curpt))
{
goto?PROGRAMEND;
}
Pause(NULL);
// cout?<“\n\n\n“;
goto?GOAHEAD
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-30?18:19??迷宮問題(棧)\
?????文件????????1301??2017-01-05?12:15??迷宮問題(棧)\Makefile.win
?????文件????????1727??2016-08-09?12:53??迷宮問題(棧)\MazeStack.cpp
?????文件?????????918??2016-12-23?23:29??迷宮問題(棧)\MazeStack.h
?????文件???????69363??2017-01-05?12:15??迷宮問題(棧)\MazeStack.o
?????文件?????????551??2017-01-05?16:46??迷宮問題(棧)\m.txt
?????文件?????????373??2017-01-03?00:09??迷宮問題(棧)\m1.txt
?????文件?????????609??2017-01-03?00:28??迷宮問題(棧)\m2.txt
?????文件????????6052??2017-01-03?00:29??迷宮問題(棧)\main.cpp
?????文件???????77707??2017-01-05?12:15??迷宮問題(棧)\main.o
?????文件????????1148??2017-01-03?04:49??迷宮問題(棧)\迷宮問題.dev
?????文件?????1992355??2017-01-05?12:15??迷宮問題(棧)\迷宮問題.exe
?????文件?????????227??2018-01-30?18:19??迷宮問題(棧)\迷宮問題.layout
評論
共有 條評論