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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.?

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute?
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.?

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

解題報告

? ?這題用bfs去遞推(其實也相當于dp啦,可以理解成? 只是不是按照for循環遍歷的順序去dp而已)

AC代碼:

#include<cstdio> #include<queue> #include<iostream> #include<cstring> #include<algorithm> #define ll long long using namespace std; const int INF = 0x3f3f3f3f; bool vis[300000 + 5]; int n,k; struct Node {int pos,step;Node(){}Node(int pos,int step):pos(pos),step(step){} }; int bfs(int x) {queue<Node> q;memset(vis,0,sizeof vis);q.push(Node(x,0));vis[x]=1;while(!q.empty()) {Node cur = q.front();q.pop();if(cur.pos == k) return cur.step;if( cur.pos+1<= 300000 && cur.pos+1 >= 0&&!vis[cur.pos+1] ){vis[cur.pos+1]=1;q.push(Node(cur.pos+1,cur.step+1));}if(cur.pos-1<= 300000&& cur.pos-1 >= 0 &&!vis[cur.pos-1] ) {vis[cur.pos-1]=1;q.push(Node(cur.pos-1,cur.step+1));}if((cur.pos<<1) <= 300000 && (cur.pos<<1) >=0 &&!vis[cur.pos<<1] ) {vis[cur.pos<<1]=1 ;q.push(Node(cur.pos<<1,cur.step+1)); }}return 0; } int main() {while(~scanf("%d%d",&n,&k)) {printf("%d\n",bfs(n));}return 0; }

錯誤代碼:(dp)

#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define ll long long using namespace std; const int INF = 0x3f3f3f3f; ll dp[3000000 +5]; inline ll min3(ll i,ll j,ll k) {if(i < j) {return i < k ? i : k;}if(j < k) return j;else return k; } int main() {int n,k;while(~scanf("%d%d",&n,&k)) {memset(dp,INF,sizeof dp);for(int i = 1; i<=n; i++) {dp[i] = min(dp[i],(ll)n-(ll)i);dp[i*2] = min(dp[i*2],dp[i]+1);}if(k <= n) {printf("%lld\n",dp[k]);continue;}for(int i = n+1; i<=(k<<1); i++) {dp[i] = min3(dp[i],dp[i-1]+ 1,dp[i+1] + 1) ;dp[i*2] = min(dp[i*2],dp[i]+1);}printf("%lld\n",dp[k]);}return 0; }

總結:

? ?這題有個很大的坑啊!!我剛開始不管數組開多大都是re,后來發現,,,數組不需要開多大,因為他有可能是負數!!所以需要判斷是否越界。

? ?其二,開30W的數組就夠了,不需要300W,但是有一點要注意,判斷條件中,必須判斷范圍在前,判斷vis數組在后。

? ? ? 即,判斷越界在前,判斷其他在后。這一點非常重要!我立馬想到了一般我寫地圖類dfs中的tx,ty也沒大注意先判斷越界還是先判斷maze是否滿足還是先判斷vis是否標記過,這是因為!!!我的地圖都是從1開始讀入的,所以就算是先判斷別的在判斷txty是否越界,也不會出現re的情況,這一點是我今天才注意到的、、慚愧慚愧、、、if中判斷順序有的時候也是有區別的!!!有可能會導致越界!!

總結

以上是生活随笔為你收集整理的【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

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