日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HDU 2822 Dogs【两次bfs】

發(fā)布時(shí)間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 2822 Dogs【两次bfs】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

6 6

..X...

XXX.X.

....X.

X.....

X.....

X.X...

3 5 6 3?

?

如上一個(gè)圖 ?告訴起點(diǎn)和終點(diǎn) ?X到達(dá)不費(fèi)力氣 ?.到達(dá)花費(fèi)1 ? 問從起點(diǎn)到終點(diǎn) ?最少的花費(fèi)力氣

分析:bfs ?遇到X在bfs

代碼:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 const int maxn = 1005; 8 char s[maxn][maxn]; 9 int vis[maxn][maxn]; 10 int n, m; 11 int x0, y0, xn, yn; 12 13 struct Node { 14 int x, y, id; 15 bool operator<(const Node &n0) const{ 16 return id > n0.id; 17 } 18 }; 19 priority_queue<Node> q, qq; 20 21 int xx[4] = { 0, 0, 1, -1 }; 22 int yy[4] = { 1, -1, 0, 0 }; 23 24 bool bfs2(Node n0) { 25 while(!qq.empty()) { 26 qq.pop(); 27 } 28 qq.push(n0); 29 Node n2; 30 while(!qq.empty()) { 31 Node n1 = qq.top(); qq.pop(); 32 if(n1.x == xn && n1.y == yn) return true; 33 for(int i = 0; i < 4; i++) { 34 int tx = n1.x + xx[i]; 35 int ty = n1.y + yy[i]; 36 if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && s[tx][ty] == 'X') { 37 vis[tx][ty] = vis[n1.x][n1.y]; 38 n2.x = tx; n2.y = ty; n2.id = vis[tx][ty]; 39 qq.push(n2); 40 q.push(n2); 41 } 42 } 43 } 44 return false; 45 } 46 47 48 void bfs() { 49 while(!q.empty()) q.pop(); 50 memset(vis, 0, sizeof(vis)); 51 vis[x0][y0] = 1; 52 Node n0 = (Node) {x0, y0, vis[x0][y0]}; 53 q.push(n0); 54 if(s[x0][y0] == 'X') { 55 if(bfs2(n0)) return; 56 } 57 Node n2; 58 while(!q.empty()) { 59 Node n1 = q.top();q.pop(); 60 if(n1.x == xn && n1.y == yn) return ; 61 for(int i = 0; i < 4; i++) { 62 int tx = n1.x + xx[i]; 63 int ty = n1.y + yy[i]; 64 if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty]) { 65 if(s[tx][ty] == 'X') { 66 vis[tx][ty] = vis[n1.x][n1.y]; 67 n2.x = tx; n2.y = ty; n2.id = vis[tx][ty]; 68 if(bfs2(n2)) return ; 69 q.push(n2); 70 } else { 71 vis[tx][ty] = vis[n1.x][n1.y] + 1; 72 n2.x = tx; n2.y = ty; n2.id = vis[tx][ty]; 73 q.push(n2); 74 } 75 } 76 } 77 } 78 } 79 80 81 int main() { 82 while(scanf("%d %d",&n, &m) ) { 83 if(n == 0 && m == 0) break; 84 for(int i = 0; i < n; i++) { 85 scanf("%s",s[i]); 86 } 87 scanf("%d %d %d %d",&x0, &y0, &xn, &yn); 88 x0--; y0--; xn--; yn--; 89 bfs(); 90 printf("%d\n", vis[xn][yn] - 1); 91 } 92 return 0; 93 } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhanzhao/p/4364015.html

總結(jié)

以上是生活随笔為你收集整理的HDU 2822 Dogs【两次bfs】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。