POJ 3278(bfs)

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31637   Accepted: 9740

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 X - 1 or X + 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.

 1 //注意下标不可变为-1,两个数组均要开到20w,为此吃了好几个re
 2 //大致题意:n可以执行加一减一乘二,求至少几步到k
 3 #include <iostream>
 4 #include <queue>
 5 #include <cstring>
 6 #include <cstdlib>
 7 using namespace std;
 8 const int N = 100000;
 9 int a[200005];
10 queue <int > q;
11 int vis[N*2+5];
12 void bfs(int n,int k)
13 {
14     vis[n] = true;
15     q.push(n);
16     while(!q.empty())
17     {
18         int head = q.front();
19         q.pop();
20         if(head==k)
21         {
22             return ;
23         }
24         else
25         {//原来没加大于0的条件,结果re
26             if(!vis[head-1]&&head<=N&&head>=1)//禁止下标变为-1
27             {
28                 vis[head-1] = true;
29                 a[head-1]+=a[head]+1;
30                 q.push(head-1);
31             }
32             if(!vis[head+1]&&head<N&&head>=0)
33             {
34                 vis[head+1] = true;
35                 a[head+1]+=a[head]+1;
36                 q.push(head+1);
37             }
38             if(!vis[head*2]&&head<=50000&&head>=0)
39             {
40                 vis[head*2] = true;
41                 a[head*2]+=a[head]+1;
42                 q.push(head*2);
43             }
44         }
45     }
46 }
47 int main()
48 {
49     int n,k;
50     cin>>n>>k;
51     memset(vis,false,sizeof(vis));
52     memset(a,0,sizeof(a));
53     while(!q.empty())
54         q.pop();
55     bfs(n,k);
56     if(n>=k)
57         cout<<n-k<<endl;
58     else
59         cout<<a[k]<<endl;
60     system("pause");
61     return 0;
62 }

 

时间: 2024-11-01 09:02:52

POJ 3278(bfs)的相关文章

poj 3984 bfs

http://poj.org/problem?id=3984 #include<iostream> #include<stdio.h> #include<cstring> #define Max 0x7f7f7f7f using namespace std; int map[6][6]; int visited[6][6]; int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}}; int ans[6][6]; int pre[30]; st

poj 3278 Catch That Cow

点击打开链接 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 62146   Accepted: 19460 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 ≤

POJ 3206 Borg Maze:BFS+Prim

Borg Maze:http://poj.org/problem?id=3026 大意:给你一个m*n的迷宫,可以上下左右的走,只能走空格或字母,求出将所有字母连通起来的最小耗费. 思路:先用BFS求出S到所有A的距离,再用Prim求最小生成树,求出最小耗费.这个题坑的不在题,是数据太坑了,在空格处理上没弄好,贡献了好几个WA和CE,看Discuss才知道很坑,最后用G++过了的代码,C++还RE,实在不知道说什么好了  =.= 更多精彩内容:http://www.bianceng.cnhttp

POJ 2251 Dungeon Master (BFS)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

POJ 1915 Knight Moves 双向BFS 入门

 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the minimum number o

poj 3414 Pots bfs+模拟

#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #define fillB 4 #define pourBA 5 #define dropB 6 #define N 10000 using namespace std; int vis[105][105]; class node { public: int A, B; int dir; node() { A=0

poj 2935 Basic Wall Maze bfs

很简单的bfs,各点都从0开始保存,可以用%和/确定x,y省空间   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <cstdio> #include <cstring> #include <queue> #define place(x,y) x+y*6 using

poj 3414 (POTS) (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12295   Accepted: 5190   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj 1724ROADS(bfs和dfs做法)

/* dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! */ #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #define Max 0x3f3f3f3f using namespace std; struct node{ int D; int L, T; node(int D, int L,