zoukankan      html  css  js  c++  java
  • poj 3278 Catch That Cow 【bfs】

    Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 52335   Accepted: 16416

    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 - 1 or + 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.

    简单bfs。 每一个状态能够转换成其它三个状态枚举就好了。

    原来是打算用spfa。 可是TL了

    代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <vector>
    const int M = 1e5+50;
    using namespace std;
    
    struct node{
    	int x;
    	int step;
    	bool operator < (const node &a) const {
    		return step > a.step;
    	}
    };
    bool vis[M];
    
    int bfs(int n, int k){
    	memset(vis, 0, sizeof(vis));
    	if(n == k) return 0;
    	node st;
    	st.x = n; st.step = 0;
    	priority_queue<node> q;
    	q.push(st);
    	while(!q.empty()){
    		node temp = q.top();
    		q.pop();
    		node cur;
    		cur.step = temp.step+1;
    		
    		cur.x = temp.x+1;
    		if(cur.x == k) return cur.step;
    		if(cur.x < M&&!vis[cur.x]){
    			vis[cur.x] = 1;
    			q.push(cur);
    		}
    		
    		cur.x = temp.x-1;
    		if(cur.x == k) return cur.step;
    		if(cur.x > 0&&cur.x < M&&!vis[cur.x]){
    			vis[cur.x] = 1;
    			q.push(cur);
    		}
    		
    		cur.x = temp.x*2;
    		if(cur.x == k) return cur.step;
    		if(cur.x < M&&!vis[cur.x]){
    			vis[cur.x] = 1;
    			q.push(cur);
    		}
    	}
    }
    
    int main(){
    	int n, k;
    	while(scanf("%d%d", &n, &k) == 2){
    		printf("%d
    ", bfs(n, k));
    	}
    }
    /*vector <int >map[M*4];
    int d[M];
    bool vis[M];
    
    /*int bfs(int n, int k){
    	memset(vis, 0, sizeof(vis));
    	priority_queue<node >q;
    	node st;
    	st.x = n; st.step = 0;
    	vis[n] = 1;
    	q.push(st);
    	if(n == k) return 0;
    	while(!q.empty()){
    		node temp = q.top();
    		q.pop();
    		node cur;
    		cur.step = temp.step+1;
    		
    		cur.x = temp.x+1;
    		if(cur.x == k) return cur.step;
    		if(cur.x > 0&&cur.x < M){
    			vis[cur.x] = 1; q.push(cur);
    		}
    		
    		cur.x = temp.x-1;
    		if(cur.x == k) return cur.step;
    		if(cur.x > 0&&cur.x < M){
    			vis[cur.x] = 1; q.push(cur);
    		}
    		
    		cur.x = temp.x*2;
    		if(cur.x == k) return cur.step;
    		if(cur.x > 0&&cur.x < M){
    			vis[cur.x] = 1; q.push(cur);
    		}
    	}
    }*/
    
    /*void spfa(int n, int k){
    	memset(vis, 0, sizeof(vis));
    	memset(d, 'a', sizeof(d));
    	queue<int >q;
    	q.push(n);
    	d[n] = 0;
    	vis[n] = 1;
    	while(!q.empty()){
    		int temp = q.front();
    		q.pop();
    		for(int i = 0; i < map[temp].size(); ++ i){
    			if(d[map[temp][i]] > d[temp]+1){
    				d[map[temp][i]] = d[temp]+1;
    				if(!vis[map[temp][i]]){
    					vis[map[temp][i]] = 1;
    					q.push(map[temp][i]);
    				}			
    			}
    		}
    	}
    }
    
    int main(){
    	int n, k;
    	while(scanf("%d%d", &n, &k) == 2){
    		for(int i = 1; i < M; ++ i){
    			map[i].clear();
    			if(i-1 > 0) map[i].push_back(i-1);
    			if(i+1 < M) map[i].push_back(i+1);
    			if(i*2 < M) map[i].push_back(i*2);
    		}
    		spfa(n, k);
    		//for(int i = 0; i < M; ++ i) vis[i] = 0, map[i].clear();
    		//for()
    		printf("%d
    ", d[k]);
    	}
    	return 0;
    }*/


  • 相关阅读:
    window下安装nvm、node.js、npm的步骤
    命令行创建Android模拟器
    2016 JavaScript 各种排名
    js开发工作流
    javascript流行工具
    Linux下进程间通信的常用方法
    Linux下回收子进程wait函数和waitpid函数的基本使用
    Linux下exec函数族比如execve等函数的基本使用
    Linux下创建子进程fork函数等的基本使用
    vi操作键盘图
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7070892.html
Copyright © 2011-2022 走看看