zoukankan      html  css  js  c++  java
  • hdoj_2717Catch That Cow

    Catch That Cow

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4193    Accepted Submission(s): 1354


    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
    有点记忆化搜索的味道=。=

    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    #define MAX 100005
    bool vis[MAX];
    typedef struct Point
    {
    	int x;
    	int cnt;
    }Point;
    queue<Point>Q;
    
    int bfs(int x, int y)
    {
    	while (!Q.empty())
    	{
    		Q.pop();
    	}
    	Point pre, next;
    	pre.x = x;
    	vis[x] = true;
    	pre.cnt = 0;
    	Q.push(pre);
    	while(!Q.empty())
    	{
    		pre = Q.front();
    		if(pre.x == y)
    		{
    			return pre.cnt;
    		}
    		Q.pop();
    
    		next.x = pre.x + 1;
    		if(next.x <= MAX && !vis[next.x])
    		{
    			vis[next.x] = true;
    			next.cnt = pre.cnt + 1;
    			Q.push(next);
    		}
    
    		next.x = pre.x - 1;
    		if(next.x >= 0 && !vis[next.x])
    		{
    			vis[next.x] = true;
    			next.cnt = pre.cnt + 1;
    			Q.push(next);
    		}
    
    		next.x = pre.x * 2;
    		if(next.x <= MAX && !vis[next.x])
    		{
    			vis[next.x] = true;
    			next.cnt = pre.cnt + 1;
    			Q.push(next);
    		}
    	}
    	return 0;
    }
    
    int main()
    {
    	int n, k;
    	while(cin >> n >> k)
    	{
    		memset(vis, false, sizeof(vis));
    		if(n >= k)
    		{
    			cout << n - k << endl;
    		}
    		else
    		{
    			cout << bfs(n, k) << endl;	
    		}
    	}
    }



  • 相关阅读:
    zookeeper 分布式锁
    kafka比较好的文章
    HTTP深入浅出 http请求
    火狐浏览器下event对象的兼容处理
    透明度轮播框架封装注意点zIndex值不能忘记
    wamp的phpmyadmin无法访问Forbidden的解决方案
    同一DIV内,两个行内块元素不对齐的解决方案
    tween.js运动曲线
    什么是闭包?闭包的优缺点?
    firefox和IE9不支持对icon font字体的跨域访问
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835121.html
Copyright © 2011-2022 走看看