在劉汝佳紫書p165
給乙個迷宮,告訴起點和終點座標,但是和平時做的迷宮不一樣,不是每個點都能走上下左右4個方向,規定了進入點的方向和從該點走出的方向(比如從北方進入a點能從a點向左或者向右轉,但是從東方進入a點就只能直走),求從起點到終點的最短走法
看到題目必然是bfs求最短路,但是這一堆條件把我搞懵了,用了半天才吃透這個題,首先,我們肯定要記錄每個點的狀態,行,列,朝向,轉向,是否走過,距出發點距離,該點的父親(以便輸出結果),將朝北東南西(順時針)走的座標變化用二維陣列表示,用乙個函式記錄轉向(前,左,右)對點朝向的影響,也用乙個函式判斷否出界,然後就開始bfs,node的資料成員為r,c,dir(行,列,朝向),判斷三種走法是否成立(是否走過,是否越界,是否有通道),如果成立就入隊,記錄該點的父親,走到終點就推出迴圈輸出答案(利用父親節點推回去就行)
#include
using
namespace std;
const
char
*dirs =
"nesw"
;const
char
*turns =
"flr"
;int next1[4]
[2]=
,,,}
;bool book[12]
[12];
int r0, c0, r1, c1, dirp, rf, cf;
bool can_turn[12]
[12][
4][3
];int d[12]
[12][
4];string name;
struct node};
node p[12]
[12][
4];vector anss;
node walk
(node u,
int turn)
if(turn ==2)
return
node
(u.r + next1[dir][0
], u.c + next1[dir][1
], dir);}
boolin(
int r,
int c)
intget_dir
(char ch)
intget_turn
(char ch)
bool
read()
}}return
true;}
void
print_ans
(node u)
anss.
push_back
(node
(r0,c0,dirp));
cout
' ';
int cnt=0;
for(
int i=
int(anss.
size()
)-1;i>=
0;i--
) cout<}void
bfs(
) q.
pop();
for(
int i =
0; i <
3; i++)}
} cout<" no solution possible"
<}int
main()
return0;
}
迷宮最短路徑
include include using namespace std const int max n 100,max m 100 const int inf 100000000 使用pair表示狀態時,使用typedef會更加方便一些 typedef pairp 輸入 char maze max ...
迷宮最短路徑
問題描述 小a同學現在被困在了乙個迷宮裡面,他很想從迷宮中走出來,他可以向上 向下 向左 向右移動 每移動一格都需要花費1秒的時間,不能夠走到邊界之外。假設小a現在的位置在s,迷宮的出口在e,迷宮可能有多個出口。問小a想要走到迷宮出口最少需要花費多少秒?並輸出從起點到最近出口的路徑。任務要求 1 迷...
ACM 迷宮的最短路徑(bfs版)
給定乙個大小為n m的迷宮,迷宮由通道和牆壁組成,每一步可以向鄰接 的上下左右四格的通道移動。請求出從起點到終點的最小部署,本題假設 從起點一定可以移動到終點 input n 10,m 10 s g output 22 include iostream include queue using nam...