码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3278 Catch That Cow 【bfs】

时间:2017-06-23 18:20:10      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:using   weight   from   ons   following   tput   width   teleport   operator   

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\n", 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\n", d[k]);
	}
	return 0;
}*/


poj 3278 Catch That Cow 【bfs】

标签:using   weight   from   ons   following   tput   width   teleport   operator   

原文地址:http://www.cnblogs.com/slgkaifa/p/7070892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!