Catch That Cow POJ-3278 BFS

题目链接:Catch That Cow

题目大意

FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点。FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两倍的地方。求FJ能找到牛的最短时间。

思路

BFS。在每一个点有三种选择,前进,后退,或者传送。要注意的是,由于有后退的过程,所以可能会造成环,导致队列长度很长就直接MLE了。因此要用一个vis数组来控制不能选择已经去过的地方。

题解

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 int n, k, ans, vis[100005];
 7 struct Location
 8 {
 9     int cur;    //当前位置坐标
10     int num;    //到达这个点所用的步数
11 }l;
12
13 queue<Location> q;
14
15 int main(int argc, char const *argv[])
16 {
17     cin >> n >> k;
18     l.cur = n;
19     l.num = 0;
20     q.push(l);    //初始位置入队
21     while(true)
22     {
23         if(q.front().cur == k)
24         {
25             cout << q.front().num;    //到达K,输出
26             break;
27         }
28         if(q.front().cur-1 >= 0 && vis[q.front().cur-1] == 0)    //判断是否超出范围或者已经走过
29         {
30             l.cur = q.front().cur-1;
31             l.num = q.front().num+1;
32             vis[l.cur] = 1;
33             q.push(l);    //入队
34         }
35         if(q.front().cur+1 <= 100000 && vis[q.front().cur+1] == 0)
36         {
37             l.cur = q.front().cur+1;
38             l.num = q.front().num+1;
39             vis[l.cur] = 1;
40             q.push(l);
41         }
42         if(q.front().cur*2 <= 100000 && vis[q.front().cur*2] == 0)
43         {
44             l.cur = q.front().cur*2;
45             l.num = q.front().num+1;
46             vis[l.cur] = 1;
47             q.push(l);
48         }
49         q.pop();
50     }
51     return 0;
52 }

原文地址:https://www.cnblogs.com/SaltyFishQF/p/10293692.html

时间: 2024-10-17 15:42:19

Catch That Cow POJ-3278 BFS的相关文章

(广搜)Catch That Cow -- poj -- 3278

链接: http://poj.org/problem?id=3278 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 62113   Accepted: 19441 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a poi

Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

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 m

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

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

poj 3278(bfs)

一条线上bfs搜就行 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int maxn=100000+100; int n,k; bool vis[maxn*2+100]; struct note { int x; int cnt; }; int bfs

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: 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

/* 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