POJ 3287 (基础BFS) Catch That Cow

这是做的第一道BFS,很基础很简单的题目

广度优先搜索算法如下:(用QUEUE)
(1) 把初始节点S0放入Open表中;
(2) 如果Open表为空,则问题无解,失败
退出;
(3) 把Open表的第一个节点取出放入
Closed表,并记该节点为n;
(4) 考察节点n是否为目标节点。若是,
则得到问题的解,成功退出;
(5) 若节点n不可扩展,则转第(2)步;
(6) 扩展节点n,将其不在Closed表和
Open表中的子节点(判重)放入Open表的尾部
,并为每一个子节点设置指向父节点的指针(
或记录节点的层次),然后转第(2)步。

一层一层的往深了的搜索,直到遇到所求解。那么深度就是最短步数,还有要注意判重,其中visited数组就起到了判重的作用。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 int n, k;
 7 const int maxn = 100000;
 8 int visited[maxn + 10];    //判重标记
 9 struct Step
10 {
11     int x;
12     int steps;
13     Step(int xx, int s):x(xx), steps(s){}
14 };
15 queue<Step> q;
16 int main(void)
17 {
18     scanf("%d%d", &n, &k);
19     memset(visited, 0, sizeof(visited));
20     q.push(Step(n, 0));
21     visited[n] = 1;
22     while(!q.empty())
23     {
24         Step s = q.front();
25         if(s.x == k)
26         {//找到目标
27             printf("%d\n", s.steps);
28             return 0;
29         }
30         else
31         {
32             if(s.x - 1 >= 0 && !visited[s.x-1])
33             {
34                 q.push(Step(s.x-1, s.steps+1));
35                 visited[s.x-1] = 1;
36             }
37             if(s.x + 1 <= maxn && !visited[s.x+1])
38             {
39                 q.push(Step(s.x+1, s.steps+1));
40                 visited[s.x+1] = 1;
41             }
42             if(s.x*2 <= maxn && !visited[s.x*2])
43             {
44                 q.push(Step(s.x*2, s.steps+1));
45                 visited[s.x*2] = 1;
46             }
47             q.pop();
48         }
49     }
50     return 0;
51 }

代码君

POJ 3287 (基础BFS) Catch That Cow,布布扣,bubuko.com

时间: 2024-08-24 10:12:56

POJ 3287 (基础BFS) Catch That Cow的相关文章

bfs Catch That Cow

#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace std; #define maxn 100001 int steps=0; int book[maxn]={0}; queue<int>q; void bfs(int a,int b) { memset(book,0

poj 3278 catch that cow BFS(基础水)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 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,板子题)

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

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 题目Catch That Cow(BFS)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52537   Accepted: 16471 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】

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52335   Accepted: 16416 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