Hdoj 2717 Catch that Cow "BFS"

Source: Internet
Author: User

Catch that Cow time limit:4000/2000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 88 Accepted Submission (s): 36Problem Description

Farmer John had 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 are at a point K (0≤ k< /c3>≤100,000) on the same number line. Farmer John has modes of transportation:walking and teleporting.

* WALKING:FJ can move from any point x to the points x -1 orx+ 1 in a single minute
* TELEPORTING:FJ can move from any point x to the point 2x 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: Nand 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 analysis: The old man to seize his cattle, already know the position of the cow, and his position, the old man can be further or step back, or transfer to the current position of the number twice times. Calculating the old man takes a few steps to catch his cow. Code:
#include <cstdio> #include <algorithm> #include <queue> #include <cstring>using namespace std; int n,m;int vis[200000];struct node{int x,y,step;} Pp;queue<node>q;int  BFs () {while (!q.empty ()) {Q.pop ();} Q.push (PP), memset (vis,0,sizeof (Vis)), Vis[pp.x]=1;while (!q.empty ()) {node Gg=q.front (); if (gg.x==m) return gg.step; Q.pop (); for (int i=0;i<3;i++) {node dd=gg;if (i==0) Dd.x=dd.x+1;else if (i==1) Dd.x=dd.x-1;else if (i==2) dd.x=dd.x*2; Dd.step++;if (dd.x==m) return dd.step;if (Dd.x>=0&&dd.x<=200000&&!vis[dd.x]) {vis[dd.x]=1; Q.push (dd);}}} Return 0;//memset (bis,0,sizeof (Vis));} int main () {while (scanf ("%d%d", &n,&m)!=eof) {         pp.x=n; pp.step=0; int Time=bfs (); printf ("%d\n", Time);} return 0;}


Copyright notice: Bo Master lover, outsiders mistakenly touch!!!

Hdoj 2717 Catch that Cow "BFS"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.