POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

题目链接:http://poj.org/problem?id=3278

 

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 124528   Accepted: 38768

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.

 题意:给你两个点 N,K,你有三种走法:X+1 ,X-1,X*2 ,求从N到K走的最少步数。

 

代码附有Wrong Answer 和 Runtime Error的几个参考原因。

 

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 
 7 /**
 8  * 造成Runtime Error的原因有两点:
 9  * 1.数组开小了,也就是下面的maxn可能只开到 1e5稍多一点,这是不对的,在搜索过程中有个 2*x ,这会导致数组大小应该大于1e5的两倍。
10  * 2.在判断!mark[x-1]的时候,没有x>0这一个约束条件,如果x = 0 ,则 x-1会导致数组越界;
11  * 3.在 x+1 和 2*x这两种情况下,应该限制 x<=K;
12  */
13  /**
14   * 造成Wrong Answer 的原因之一:在判断 x-1 的情况的时候,不要有 x<=K
15   */
16 const int maxn = 2e5+100;
17 bool mark[maxn];
18 int N,K;
19 struct node
20 {
21     int x;
22     int step;
23 };
24 
25 //队列的写法;
26 void bfs()
27 {
28     queue<node> q;
29     struct node cu,ne;
30     cu.x = N;
31     cu.step = 0;
32     mark[N] = 1;
33     q.push(cu);
34     while(!q.empty())
35     {
36         cu = q.front();
37         q.pop();
38         if(cu.x == K)
39         {
40             printf("%d\n",cu.step);
41             return ;
42         }
43         //下面的三种情况应该是并列关系,不应该满足一种情况就不判断别情况了
44         //我开始写成了if-else if-else if形式,想成不是并列的形式了;
45         //如果实在找不到哪里错了,可以打印cu.x变量,根据变量的值找问题;
46         if(cu.x>0&&!mark[cu.x-1])
47         {
48             ne.x = cu.x - 1;
49             ne.step = cu.step + 1;
50             mark[ne.x] = 1;
51             q.push(ne);
52         }
53         if(cu.x<=K&&!mark[cu.x+1])
54         {
55             ne.x = cu.x + 1;
56             ne.step = cu.step + 1;
57             mark[ne.x] = 1;
58             q.push(ne);
59         }
60         if(cu.x<=K&&!mark[cu.x*2])
61         {
62             ne.x = 2*cu.x;
63             ne.step = cu.step + 1;
64             mark[ne.x] = 1;
65             q.push(ne);
66         }
67     }
68 }
69 int main()
70 {
71     while(scanf("%d%d",&N,&K)!=EOF)
72     {
73         memset(mark,0,sizeof(mark));
74         bfs();
75     }
76     return 0;
77 }
Ac代码

 

posted @ 2018-11-27 09:02  HiCYP  阅读(334)  评论(0编辑  收藏  举报