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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

發布時間:2024/9/5 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前寫的題了,現在想整理一下,就掛出來了。

題意比較明確,給一張n*m的地圖,從左上角(0, 0)走到右下角(n-1, m-1)。

'X'為墻,'.'為路,數字為怪物。墻不能走,路花1s經過,怪物需要花費1s+數字大小的時間。

比較麻煩的是需要記錄路徑。還要記錄是在走路還是在打怪。

因為求最短路,所以可以使用bfs。

因為進過每一個點花費時間不同,所以可以使用優先隊列。

因為需要記錄路徑,所以需要開一個數組,來記錄經過節點的父節點。當然,記錄方法不止一種。

?

上代碼——

1 #include <cstdio> 2 #include <cstring> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 9 struct node 10 { 11 int x, y, step; 12 bool operator < (const node& a) const 13 { 14 return a.step < step; 15 } 16 }; 17 18 int go[4][2] = {{1, 0},{-1, 0}, {0, 1}, {0, -1}}; 19 20 int n, m, step; 21 bool v[110][110]; 22 char mp[110][110]; 23 int last[110][110]; 24 25 bool bfs() 26 { 27 node p, q; 28 p.x = n-1; 29 p.y = m-1; 30 p.step = 0; 31 if(mp[n-1][m-1] >= '0' && mp[n-1][m-1] <= '9') p.step += mp[n-1][m-1] -'0'; 32 33 priority_queue <node> que; 34 if(mp[p.x][p.y] != 'X') 35 que.push(p); 36 v[p.x][p.y] = 1; 37 38 while(!que.empty()) 39 { 40 p = que.top(); 41 que.pop(); 42 43 for(int i = 0; i < 4; i++) 44 { 45 int x = p.x+go[i][0]; 46 int y = p.y+go[i][1]; 47 48 if(x >= 0 && x < n && y >= 0 && y < m && !v[x][y]) 49 { 50 if(mp[x][y] == 'X') continue; 51 52 q.x = x; q.y = y; q.step = p.step+1; 53 if(mp[x][y] >= '1' && mp[x][y] <= '9') q.step += mp[x][y]-'0'; 54 que.push(q); 55 v[x][y] = 1; 56 last[x][y] = p.x*1000+p.y; 57 if(x == 0 && y == 0) {step = q.step; return 1;} 58 59 } 60 } 61 } 62 return 0; 63 } 64 65 void output() 66 { 67 printf("It takes %d seconds to reach the target position, let me show you the way.\n", step); 68 int x = 0; 69 int y = 0; 70 int i = 1; 71 while(x != n-1 || y != m-1) 72 { 73 if(mp[x][y] >= '1' && mp[x][y] <= '9') 74 { 75 int stop = mp[x][y] - '0'; 76 while(stop--) 77 { 78 printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y); 79 } 80 } 81 printf("%ds:(%d,%d)->(%d,%d)\n", i++, x, y, last[x][y]/1000, last[x][y]%1000); 82 83 int t = last[x][y]/1000; 84 y = last[x][y]%1000; 85 x = t; 86 } 87 if(mp[x][y] >= '1' && mp[x][y] <= '9') 88 { 89 int stop = mp[x][y] - '0'; 90 while(stop--) 91 { 92 printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y); 93 } 94 } 95 } 96 97 int main() 98 { 99 //freopen("test.txt", "r", stdin); 100 while(~scanf("%d%d", &n, &m)) 101 { 102 memset(mp, 0, sizeof(mp)); 103 memset(v, 0, sizeof(v)); 104 memset(last, 0, sizeof(last)); 105 for(int i = 0; i < n; i++) 106 { 107 scanf("%s", mp[i]); 108 } 109 110 if(bfs()) output(); 111 else printf("God please help our poor hero.\n"); 112 printf("FINISH\n"); 113 } 114 return 0; 115 } View Code

?

轉載于:https://www.cnblogs.com/mypride/p/4695168.html

總結

以上是生活随笔為你收集整理的hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。