hdu 2717 (一维广搜)

    xiaoxiao2021-03-25  69

    Catch That Cow

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14027    Accepted Submission(s): 4293 Problem Description 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.

    #include<iostream> #include<queue> #include<memory.h> using namespace std; int n,m; int vis[100005]={0}; int dir[2]={-1,1}; struct node { int x; int step; }s,e; void bfs() { queue<node>q; node x1,t; s.step=0; vis[s.x]=1; q.push(s); while(!q.empty()) { t=q.front(); //cout<<t.x<<endl; q.pop(); if(t.x==e.x) { cout<<t.step<<endl; return; } else { for(int i=0;i<2;i++) { x1.x=t.x+dir[i]; if(x1.x>=0&&x1.x<=100000&&vis[x1.x]==0) { x1.step=t.step+1; vis[x1.x]=1; q.push(x1); } } x1.x=t.x*2; if(x1.x>=0&&x1.x<=100000) { x1.step=t.step+1; vis[x1.x]=1; q.push(x1); } } } cout<<-1<<endl; } int main() { while(cin>>n>>m) { memset(vis,0,sizeof(vis)); s.x=n; e.x=m; bfs(); } }

    转载请注明原文地址: https://ju.6miu.com/read-35443.html

    最新回复(0)