poj 3278 Catch That Cow,poj3278

來源:互聯網
上載者:User

poj 3278 Catch That Cow,poj3278
Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 56   Accepted Submission(s) : 20Problem Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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?

 
InputLine 1: Two space-separated integers:N and K 
OutputLine 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
 
SourcePKU搜尋

 

思路:

       這道題是一維座標上給出起點和終點,然後讓你用兩種(也可以說是三種)不同的方式進行移動,看話費的最少時間是多少(也可以說是最小的步數是多少)!需要注意的是步行可以左右移動,但是瞬移(也就是另一種移動方式只能往前走),然後你用這三種方式對一維座標中的點進行廣搜,最小用多少步能夠到達終點!

      這道題與之前做的題不同之處就是,之前的是在二維平面上進行廣搜(有不同的路徑),找最短的路徑,而這道題雖然只有一條路徑可以走,但是題目又給了一個限制,也就是可以有不同的行走方式,可以一步進行跨越式的走,所以就這樣增加了路徑的個數,所以這道題其實也是讓求最短的路徑的!

     一般上我們做求最短的路徑的問題,我們就用廣搜的方法!

 具體代碼如下:

 

#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;#define INF 0xfffffffint n,m,ans;int vis[100005];struct node {int x;int time;friend bool operator < (node a,node b){return a.time>b.time;}}a,temp;int judge(){if(temp.x<0||temp.x>100000)return 0;if(vis[temp.x]==1)return 0;if(temp.time>=ans)return 0;return 1;}void bfs(){a.x=n;//將起點的值賦值給結構體a然後將結構體a放到對列中,來尋找它周圍的點(前後和2倍的這三個點) a.time=0;//剛開始時間是0! priority_queue<node>q;//設一個優先隊列,來將最短的時間放到對頂 q.push(a);//將數組a壓到隊列裡面 memset(vis,0,sizeof(vis));//清空標記數組 vis[n]=1;//起點進行標記 while(!q.empty())//如果隊列不為空白,進行下面的操作,來找終點 {a=q.top();//因為下面要用到對頂元素,所以只有隊列不為空白的時候,下面的操作才有意義! q.pop();//賦值過之後出隊列,因為你之後找過它周圍的數之後,它已經沒用了,就出隊列吧 for(int i=0;i<3;i++)//三種不同的方式來尋找終點 {if(i==0)temp.x=a.x+1;if(i==1)temp.x=a.x-1;if(i==2)temp.x=a.x*2;temp.time=a.time+1;//每走一步,對應的時間要加一 if(judge())//判斷這個點是否符合要求,如果符合,則判斷是不是要找的點 {if(temp.x==m)//如果是要找的點要將到達這個點所用的時間輸出 {ans=temp.time;return;}//如果不是最終的點,需要將這個點標記一下,並且放進對列,找它周圍有沒有終點,一直 vis[temp.x]=1;//迴圈,知道找到終點為止! q.push(temp);}}   }} int main(){while(scanf("%d%d",&n,&m)!=EOF){        ans=INF;        if(n==m)//因為起點和終點重合的時候,起點之前被標記了,而(在bfs中)終點要求不能夠被標記,所以         {//所以要在進bfs之前進行判斷是否起點和終點重合!           printf("0\n");          continue;            }bfs();//調用dfs求最短時間(也就是最短路徑)! printf("%d\n",ans);}return 0;}


 

 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.