hdu 2717 Catch That Cow

来源:互联网 发布:js new做了什么 编辑:程序博客网 时间:2024/06/08 18:14
<h1 style="text-align: center; color: rgb(26, 92, 200);">Catch That Cow</h1><span size="+0" style=""></span><div style="text-align: center;"><span style="color: green; font-family: Arial; font-weight: bold;">Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)</span></div><strong></strong><div style="text-align: center;"><span style="color: green; font-family: Arial;">Total Submission(s): 10101    Accepted Submission(s): 3157</span></div><div class="panel_title" align="left">Problem Description</div><div class="panel_content">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?</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Input</div><div class="panel_content">Line 1: Two space-separated integers: N and K</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Output</div><div class="panel_content">Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="FONT-FAMILY: Courier New,Courier,monospace">5 17</div>

#include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f using namespace std;int s,e;struct Location{int site;int time;} n,t;int  mark[1000000];int bfs(){t.site=s;t.time=0;memset(mark,0,sizeof(mark));queue<Location>q;q.push(t);mark[s]=1;while(!q.empty()){t=q.front();q.pop();for(int i=1;i<=3;i++){if(i==1)   n.site=t.site+1;if(i==2)   n.site=t.site-1;if(i==3)   n.site=t.site*2;n.time=t.time+1;if(n.site==e)   return n.time;if(n.site<0||n.site>1000000)   continue;if(!mark[n.site]) { mark[n.site]=1; q.push(n); }    }} return INF;}int main(){while(scanf("%d%d",&s,&e)!=EOF){if(e<s)  printf("%d\n",s-e);else if(s==e)   printf("0\n");else  printf("%d\n",bfs());}return 0;}

0 0