POJ-3278 Catch That Cow【广度优先搜索】
来源:互联网 发布:手机联机软件 编辑:程序博客网 时间:2024/05/18 20:11
1.思路分析
广度优先搜索。
2.方法设计及性能衡量
用队列来进行压入,可以很快求出答案,注意如果N,K相同输出0
3.实现部分
#include<iostream>#include<cstdio>#include<string>#include<stdlib.h>#include<math.h>#include<queue>#include<algorithm>using namespace std;int dp[100010],N,K;queue<int>que;int main(){ while(scanf("%d%d",&N,&K)!=EOF){ que.push(N); while(!que.empty()){ if(N==K){ printf("%d\n",0); break; } if(dp[K]){ printf("%d\n",dp[K]); break; } int temp=que.front();que.pop(); if(temp!=0&&temp*2<100010&&!dp[temp*2]){ que.push(temp*2); dp[temp*2]=dp[temp]+1; } if(temp-1>=0&&!dp[temp-1]){ que.push(temp-1); dp[temp-1]=dp[temp]+1; } if(temp+1<100010&&!dp[temp+1]){ que.push(temp+1); dp[temp+1]=dp[temp]+1; } } } return 0;}
阅读全文
0 0
- POJ 3278 Catch That Cow(BFS广度优先搜索)
- POJ 3278-Catch That Cow 广度优先搜索BFS
- poj 3278 Catch That Cow 广度优先搜索
- Catch That Cow POJ 3278 BFS 广度优先搜索
- POJ-3278 catch that cow 广度优先搜索
- POJ-3278 Catch That Cow【广度优先搜索】
- poj3287 Catch That Cow(广度优先搜索)
- Catch That Cow-----广度优先搜索
- POJ3278 - Catch That Cow - 广度优先搜索
- Catch That Cow(广度优先搜索_bfs)
- Catch That Cow-广度优先搜索-POJ3278
- Catch That Cow广度优先搜索
- poj 3278 Catch That Cow 广度优先搜索 + 回溯 队列 剪枝
- poj 3278 catch that cow (广度优先BFS)
- poj 3278 Catch That Cow 广度优先算法
- POJ3278 Catch That Cow 广度优先搜索版
- poj3278 hdu2717 Catch That Cow 广度优先搜索
- (广度优先搜索)算法题-poj3278 Catch That Cow
- A/B(HDU1576)
- 我眼中的代理模式
- Python玩转微信 个性签名生成词云图
- glRasterPos2f函数介绍
- 个人总结操作符的特性,包括:按位操作符、位移操作符和单目操作符中的sizeof
- POJ-3278 Catch That Cow【广度优先搜索】
- 【机器学习算法】从决策树到GBDT(一)
- Linux 安装VMware-Tools
- 访问矩阵中的数据的两种方法
- 自定义RecyclerView之点击事件
- 树状数组模版
- sublime text 3
- volatile和transient
- angularjs解析数据展示在表格两种方法