M - Catch That Cow POJ - 3278

拼搏现实的明天。 2022-06-15 01:56 251阅读 0赞

Think:
1BFS
2反思:注意数组越界+注意标记走过的点
3C++中的STL的queue练习

M - Catch That Cow POJ - 3278

  1. 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.
  2. * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
  3. * Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
  4. 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

  1. 5 17

Sample Output

  1. 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.

以下为Runtime Error代码——数组越界

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. struct node{
  6. int x;
  7. int step;
  8. }a, b;
  9. int v[240000];
  10. void BFS(int n, int m);
  11. int main(){
  12. int n, m;
  13. while(scanf("%d %d", &n, &m) != EOF){
  14. BFS(n, m);
  15. }
  16. return 0;
  17. }
  18. void BFS(int n, int m){
  19. queue<struct node> q;
  20. a.x = n;
  21. a.step = 0;
  22. q.push(a);
  23. v[n] = 1;
  24. while(!q.empty()){
  25. a = q.front();
  26. q.pop();
  27. if(a.x == m){
  28. printf("%d\n", a.step);
  29. break;
  30. }
  31. if(a.x < m && v[a.x+1] == 0){
  32. b.x = a.x + 1;
  33. b.step = a.step + 1;
  34. q.push(b);
  35. v[b.x] = 1;
  36. }
  37. if(a.x >= 1 && v[a.x-1] == 0){
  38. b.x = a.x - 1;
  39. b.step = a.step + 1;
  40. q.push(b);
  41. v[b.x] = 1;
  42. }
  43. if(v[a.x*2] == 0){
  44. b.x = a.x*2;
  45. b.step = a.step + 1;
  46. q.push(b);
  47. v[b.x] = 1;
  48. }
  49. }
  50. }

以下为Accepted代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. struct node{
  6. int x;
  7. int step;
  8. }a, b;
  9. int v[240000];
  10. void BFS(int n, int m);
  11. int main(){
  12. int n, m;
  13. while(scanf("%d %d", &n, &m) != EOF){
  14. BFS(n, m);
  15. }
  16. return 0;
  17. }
  18. void BFS(int n, int m){
  19. queue<struct node> q;
  20. a.x = n;
  21. a.step = 0;
  22. q.push(a);
  23. v[n] = 1;
  24. while(!q.empty()){
  25. a = q.front();
  26. q.pop();
  27. if(a.x == m){
  28. printf("%d\n", a.step);
  29. break;
  30. }
  31. if(a.x < m && v[a.x+1] == 0){
  32. b.x = a.x + 1;
  33. b.step = a.step + 1;
  34. q.push(b);
  35. v[b.x] = 1;
  36. }
  37. if(a.x >= 1 && v[a.x-1] == 0){
  38. b.x = a.x - 1;
  39. b.step = a.step + 1;
  40. q.push(b);
  41. v[b.x] = 1;
  42. }
  43. if(a.x <= m && v[a.x*2] == 0){
  44. b.x = a.x*2;
  45. b.step = a.step + 1;
  46. q.push(b);
  47. v[b.x] = 1;
  48. }
  49. }
  50. }

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读