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\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;
}*/
时间: 2024-12-06 10:55:42

poj 3278 Catch That Cow 【bfs】的相关文章

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10

POJ - 3278 - Catch That Cow (BFS)

题目传送:Catch That Cow 思路:BFS找最小步数,用一个结构体存下当前结点的数值以及当前步数 AC代码: #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <queue>

hdoj 2717 Catch That Cow【bfs】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9276    Accepted Submission(s): 2907 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdoj 2717 Catch That Cow 【bfs】

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 88   Accepted Submission(s) : 36 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to c

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 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,00

poj 3278:Catch That Cow(简单一维广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 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,00

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok