POJ 3278(Catch That Cow)
1 /* BFS入门题 2 思路:以n为始点开始搜索,直到搜到k为止 3 注意需剪枝,否则会超时,剪枝最重要的一点就是: 4 已经入队过得点不许再重复入队 5 */ 6 #include<iostream> 7 #include<queue> 8 using namespace std; 9 bool flag[200005]; 10 struct node 11 { 12 int x,t; 13 }; 14 void BFS(int n,int k) 15 { 16 queue<node> q; 17 node a; 18 memset(flag,false,sizeof(flag)); 19 a.x = n; 20 a.t = 0; 21 q.push(a); 22 flag[n] = true; 23 while(!q.empty()) 24 { 25 node temp = q.front(); 26 q.pop(); 27 node next; 28 if(temp.x-1 == k) 29 { 30 printf("%d\n",temp.t+1); 31 return; 32 } 33 if(temp.x >= 1 && !flag[temp.x-1]) 34 { 35 next.x = temp.x-1,next.t = temp.t+1; 36 q.push(next); 37 flag[temp.x-1] = true; 38 } 39 if(temp.x+1 == k) 40 { 41 printf("%d\n",temp.t+1); 42 return; 43 } 44 if(temp.x <= k && !flag[temp.x+1]) 45 { 46 next.x = temp.x+1,next.t = temp.t+1; 47 q.push(next); 48 flag[temp.x+1] = true; 49 } 50 if(temp.x * 2 == k) 51 { 52 printf("%d\n",temp.t+1); 53 return; 54 } 55 if(temp.x < k && !flag[temp.x*2]) 56 { 57 next.x = temp.x*2,next.t = temp.t+1; 58 q.push(next); 59 flag[temp.x*2] = true; 60 } 61 } 62 } 63 int main() 64 { 65 int n,k; 66 //freopen("3278.txt","r",stdin); 67 while(scanf("%d%d",&n,&k) != EOF) 68 { 69 if(n == k) 70 { 71 printf("0\n"); 72 continue; 73 } 74 BFS(n,k); 75 } 76 return 0; 77 }
转载于:https://www.cnblogs.com/sorryhao/archive/2012/09/27/2705330.html
POJ 3278(Catch That Cow)相关推荐
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35043 Accepted: 10800 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- bfs+dfs分析----poj 3278 Catch That Cow
题目详情 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 115430 Accepted: ...
- poj 3278 Catch That Cow 广搜
hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- poj 3278 Catch That Cow(广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45087 Accepted: 14116 ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
最新文章
- ApacheBench-web性能测试
- ASP.NET中常用的优化性能方法(转)
- 单开双控_卧室或者楼梯灯怎么安装单联双控
- ArrayList的使用
- 最小的linux服务器_学习以最小的努力构建GraphQL服务器
- foreach之引用的使用
- react使用antd实现日期与时间的不可选定
- B00012 C++算法库的sort()函数
- orangepi自启动打开一个终端并且运行脚本
- Elastic Search 学习笔记
- 时间序列谐波分析(HANTS)的MATLAB实现
- qq飞车 ios android,《QQ飞车手游》ios和安卓数据互通吗 安卓苹果互通规则详解
- Type-C潮流下 如何衡量一款数据线好坏?
- Python——代码界的大门之一
- 功能测试--如何对时间(年月日)进行测试
- mysql 关联查询
- lo4net的简单运用
- Zuul入门实战(完整版)
- 使用Vmalert监控报警
- 空压机变频改造|空气压缩机变频改造原理
热门文章
- this指向问题 php,js中的this指向问题
- 数学建模_混合赌博模型
- c语言打印字符数据在屏幕上,在屏幕上输出各种类型的数据
- 中条码一般为四色怎么转单色黑_条码机适用的耗材判断标准
- matlab 结构体遍历,matlab 遍历结构体struc的成员
- java IEnumerator_获取实现接口的所有类型
- 11矩阵的QR分解(1)
- 双极性根升余弦信号qpsk_基于CCSDS协议的中频信号源设计与实现
- 机器学习中的numpy的array_Python中机器学习的Numpy指南
- matlab聚类分析_数据挖掘——聚类分析