poj3278简单BFS
来源:互联网 发布:寂寞聊天软件 编辑:程序博客网 时间:2024/06/06 17:16
题目链接:点击打开链接
题意;给你一个n,k.有三种操作,1.n->n-1 2.n->n+1 3.n->2n 花费一个时间,问你从n->k要多少时间
思路:BFS一次到k就可以了
#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn=1e5+10;int a[maxn],n,k;int check(int x){ if(x>=0&&x<=1e5)return 1; return 0;}void BFS(){ int temp,now; a[n]=0; queue<int>q; q.push(n); while(!q.empty()) { temp=q.front(); q.pop(); now=temp-1; if(check(now)) if(a[now]>a[temp]+1) { a[now]=a[temp]+1; q.push(now); } if(now==k)return; now=temp+1; if(check(now)) if(a[now]>a[temp]+1) { a[now]=a[temp]+1; q.push(now); } if(now==k)return; now=temp*2; if(check(now)) if(a[now]>a[temp]+1) { a[now]=a[temp]+1; q.push(now); } if(now==k)return; }}int main(){ while(~scanf("%d %d",&n,&k)) { memset(a,0x3f,sizeof(a)); BFS(); printf("%d\n",a[k]); } return 0;}
阅读全文
0 0
- POJ3278 简单BFS+剪枝
- poj3278简单BFS
- poj3278(简单的bfs)
- POJ3278 Catch That Cow 简单BFS
- poj3278 BFS
- POJ3278---bfs
- poj3278 bfs
- poj3278(bfs)
- POJ3278 -- BFS
- POJ3278--BFS
- poj3278-bfs
- POJ3278 BFS
- poj3278 bfs
- POJ3278 BFS
- poj3278 Catch That Cow 水BFS+简单剪枝
- 照葫芦画瓢的BFS,poj3278
- POJ3278 BFS 水题
- 0.8poj3278(bfs)
- 使用requests模拟登陆csdn
- DataIntegrityViolationException
- 手持端手机端移动端车牌识别介绍
- va_list及vsnprintf的用法
- GP TEE标准体系一张图
- poj3278简单BFS
- 位运算---整数的二进制表达中有多少个1
- 剑指offer
- JavaScript引入错误
- 选择排序
- Java计算两点间的距离
- android sdk 镜像下载方法
- 53. Maximum Subarray
- 框架导出word