?? algo0303.cpp
字號(hào):
Status Pass(MazeType MyMaze, PosType CurPos);
void FootPrint(MazeType &MyMaze, PosType CurPos);
void MarkPrint(MazeType &MyMaze, PosType CurPos);
PosType NextPos(PosType CurPos, int Dir);
Status MazePath(MazeType &maze, PosType start, PosType end) {
// 算法3.3
// 若迷宮maze中從入口 start到出口 end的通道,則求得一條存放在棧中
// (從棧底到棧頂),并返回TRUE;否則返回FALSE
Stack S;
PosType curpos;
int curstep;
SElemType e;
InitStack(S);
curpos = start; // 設(shè)定"當(dāng)前位置"為"入口位置"
curstep = 1; // 探索第一步
do {
if (Pass(maze,curpos)) { // 當(dāng)前位置可通過(guò),即是未曾走到過(guò)的通道塊
FootPrint(maze,curpos); // 留下足跡
e.di =1;
e.ord = curstep;
e.seat= curpos;
Push(S,e); // 加入路徑
if (curpos.r == end.r && curpos.c==end.c)
return (TRUE); // 到達(dá)終點(diǎn)(出口)
curpos = NextPos(curpos, 1); // 下一位置是當(dāng)前位置的東鄰
curstep++; // 探索下一步
} else { // 當(dāng)前位置不能通過(guò)
if (!StackEmpty(S)) {
Pop(S,e);
while (e.di==4 && !StackEmpty(S)) {
MarkPrint(maze,e.seat);
Pop(S,e); // 留下不能通過(guò)的標(biāo)記,并退回一步
} // while
if (e.di<4) {
e.di++;
Push(S, e); // 換下一個(gè)方向探索
curpos = NextPos(e.seat, e.di); // 當(dāng)前位置設(shè)為新方向的相鄰塊
} // if
} // if
} // else
} while (!StackEmpty(S) );
return FALSE;
} // MazePath
Status Pass( MazeType MyMaze,PosType CurPos) {
if (MyMaze.arr[CurPos.r][CurPos.c]==' ')
return 1; // 如果當(dāng)前位置是可以通過(guò),返回1
else return 0; // 其它情況返回0
}
void FootPrint(MazeType &MyMaze,PosType CurPos) {
MyMaze.arr[CurPos.r][CurPos.c]='*';
}
void MarkPrint(MazeType &MyMaze,PosType CurPos) {
MyMaze.arr[CurPos.r][CurPos.c]='!';
}
PosType NextPos(PosType CurPos, int Dir) {
PosType ReturnPos;
switch (Dir) {
case 1:
ReturnPos.r=CurPos.r;
ReturnPos.c=CurPos.c+1;
break;
case 2:
ReturnPos.r=CurPos.r+1;
ReturnPos.c=CurPos.c;
break;
case 3:
ReturnPos.r=CurPos.r;
ReturnPos.c=CurPos.c-1;
break;
case 4:
ReturnPos.r=CurPos.r-1;
ReturnPos.c=CurPos.c;
break;
}
return ReturnPos;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -