!POJ 3278 Catch That Cow--BFS(隐蔽的BFS)

题意:两个整数,N 和 M,N有三种变换:-1  ,+1 , *2 ,求N通过这三种变换变为M的最小的次数

分析:这题用BFS。

这题的问题主要是:

1.难以想到用BFS来做

知道用BFS之后就很快的写出来代码,但是还是出错了:

2.TLE。原因是没有标记出现过的数,广搜一定要记得标记

3.RE。  N的范围在0到1000000,要把N的范围限制在这里面

4.WA。有一种特殊情况 N=M,要单独讨论(BFS和DFS出现WA的时候如果算法都没错那基本上就是一些特殊情况没有考虑到,所以做题的时候最好先把这些情况提出来)

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
struct h{
	int w,g;
};
queue<h> q;
int cnt;
int vis[1000009];
int bfs()
{
	h tmp;
	int t;
	while(!q.empty()){
		tmp=q.front();
		t=tmp.g;
		q.pop();
		h tmp2;
		for(int i=0;i<3;i++){
			switch(i){
				case 0:tmp2.w=tmp.w-1;break;
				case 1:tmp2.w=tmp.w+1;break;
				default:tmp2.w=2*tmp.w;
			}
			if(tmp2.w>=0&&tmp2.w<=100000){
			if(tmp2.w==m) return t+1;
			if(!vis[tmp2.w]){
			vis[tmp2.w]=1;
			tmp2.g=t+1;
			q.push(tmp2);}}
		}
	}
}
int main()
{
	memset(vis,0,sizeof(vis));
	cin>>n>>m;
	if(n==m) cout<<"0"<<endl;
	else{

	h tmp;
	tmp.w=n,tmp.g=0;
	vis[n]=1;
	q.push(tmp);
	cnt=bfs();
	cout<<cnt<<endl;}
	return 0;
}
时间: 2025-01-01 13:44:11

!POJ 3278 Catch That Cow--BFS(隐蔽的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(广搜)

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 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 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(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>

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,00

poj 3278 Catch That Cow (bfs搜索)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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