poj3278(Catch That Cow)

题目大意:

一个农主寻找牛。给出农主的位置n和牛的位置k。农主可以通过n-1或者n+1或者n*2的步伐找牛,问至少多少步才能找到自己的牛。

解题思路:

简单的BFS。把农主的每一种可能的步伐通过BFS存到栈中,然后看最少多少步到达K坐标。

代码:

 1 #include <algorithm>
2 #include <iostream>
3 #include <sstream>
4 #include <cstdlib>
5 #include <cstring>
6 #include <cstdio>
7 #include <string>
8 #include <bitset>
9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <list>
14 #include <map>
15 #include <set>
16 using namespace std;
17 /***************************************/
18 #define ll long long
19 #define int64 __int64
20 /***************************************/
21 const int INF = 0x7f7f7f7f;
22 const double eps = 1e-8;
23 const double PIE=acos(-1.0);
24 const int dx[]= {0,-1,0,1};
25 const int dy[]= {1,0,-1,0};
26 const int fx[]= {-1,-1,-1,0,0,1,1,1};
27 const int fy[]= {-1,0,1,-1,1,-1,0,1};
28 /***************************************/
29 void openfile()
30 {
31 freopen("data.in","rb",stdin);
32 freopen("data.out","wb",stdout);
33 }
34 /**********************华丽丽的分割线,以上为模板部分*****************/
35 int vis[200009],cnt[200009];//如果你错了,可以开大点数组试试。
36 int BFS(int n,int k)
37 {
38 queue<int >Q;
39 Q.push(n);
40 int v;
41 while(!Q.empty())
42 {
43 v=Q.front();
44 Q.pop();
45 if (vis[v]==-1)
46 continue;
47 vis[v]=-1;
48 if (v==k)
49 return cnt[k];
50 if (v<100005&&v>-2)
51 {
52 Q.push(v-1);
53 if (!cnt[v-1])
54 cnt[v-1]=cnt[v]+1;
55 Q.push(v+1);
56 if (!cnt[v+1])
57 cnt[v+1]=cnt[v]+1;
58 Q.push(v*2);
59 if (!cnt[v*2])
60 cnt[v*2]=cnt[v]+1;
61
62 }
63 }
64 return 0; //为什么没这句,结果就不对?
65 }
66 int main()
67 {
68
69 int n,k,sum;
70 while(scanf("%d%d",&n,&k)!=EOF)
71 {
72 memset(vis,0,sizeof(vis));
73 memset(cnt,0,sizeof(cnt));
74 sum=BFS(n,k);
75 printf("%d\n",sum);
76 }
77 return 0;
78 }

poj3278(Catch That Cow),布布扣,bubuko.com

时间: 2024-10-21 03:26:34

poj3278(Catch That Cow)的相关文章

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

POJ3278,Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46459   Accepted: 14574 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (0 ≤ N ≤ 100,000

【poj3278】Catch That Cow

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 Jo

【搜索】POJ3278:Catch That Cow

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 Jo

Core Java 经典笔试题总结(异常类问题)

所有代码均在本地编译运行测试,环境为 Windows7 32位机器 + eclipse Mars.2 Release (4.5.2) 2016-10-17 整理 下面的代码输出结果是多少?为什么?并由此总结几个编程规范. 1 class smallT { 2 public static void main(String args[]) { 3 smallT t = new smallT(); 4 int b = t.get(); 5 System.out.println(b); 6 } 7 8

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

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(模板——BFS)

题目链接:http://poj.org/problem?id=3278 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