poj 3009 深搜求最短路

2022-07-08 02:57:07 字數 1151 閱讀 1987

題目大意就是求在特定規則下的最短路,這個規則包含了消除障礙的操作。用bfs感覺選擇消除障礙的時候不同路徑會有影響,用dfs比較方便狀態的還原(雖然效率比較低),因此這道題目採用dfs來寫。

寫的第一次提交**是tle,原因是忘記總步數》10就應該剪枝的條件。ac**如下:

1 #include 2 #include 3 #include 4

using

namespace

std;

5const

int maxn = 22;6

struct

point9};

10point s,t;

11int

w,h;

12int

g[maxn][maxn];

13const

int dr = ;

14const

int dc = ;

15int ans = 100000;16

void dfs(int r,int c,int

k)30}31

if(!is_walk)continue;32

if(g[nr+dr[i]][nc+dc[i]] == 4)continue;33

if(g[nr+dr[i]][nc+dc[i]] == 1)38

}39}40

intmain()

47for(int i = 0; i <= w+1; i++)

48 g[0][i] = 4,g[h+1][i] = 4;49

for(int i = 0; i <= h+1; i++)

50 g[i][0] = 4,g[i][w+1] = 4;51

for(int i = 1; i <= h; i++)

57else

if (g[i][j] == 3)61

}62}63 dfs(s.r,s.c,0

);64

if(ans != 100000 && ans <= 10)printf("

%d\n

",ans);

65else printf("

%d\n

",-1

);66}67

return0;

68 }

poj 3009 dfs暴力解決最短路

題目就不粘了,題意是這樣的 乙個帶網格的方格板,有兩種方格,一種是白色的,一種是網狀的。白色表示可以通過,用數字0表示,網狀的表示障礙物不能通過,用數字1表示。起點和終點分別在兩個白色的上面,分別用數字2 3表示。目標就是從起點走到終點,且步數小於等於10。走法和步數計算比較特殊 選定乙個方向後,只...

POJ 3009 簡單的dfs回溯遍歷搜尋)

注意行 與 列 的輸入時反過來的 然後就是障礙物被撞後會消失 但當回來時要恢復被撞的障礙物。只用了乙個簡單的剪枝 當區域性最優值大於當前最優值則剪枝 include include include include include include include include include inc...

poj 3009 冰壺 DFS 乙個方向搜查到底

題意 冰壺可以上下左右運動 前提上下左右的第乙個位置為空 碰到冰塊則停止,冰塊也將被破壞。問懂多少次能達到終點。乙個方向搜到底 include include using namespace std int map 30 30 int w,h int minn 300000 void dfs int...