poj 3278 -- Catch That Cow (最短路)
来源:互联网 发布:敬请悉知 编辑:程序博客网 时间:2024/05/17 07:09
建图,以n为源点,n-1,n+1,2*n分别与n建立权值为1的边,题目即转化为了求n到k的最短路
注意搜索加限定条件,容易re
#include<queue>#include<iostream>using namespace std;int a[100001]={0};int bfs(int n,int k){ a[n]=0; queue<int> q; q.push(n); int t=-1; while(t!=k) { t=q.front(); if(t-1>=0&&a[t-1]==0) a[t-1]=a[t]+1,q.push(t-1); if(t+1<=100000&&a[t+1]==0) a[t+1]=a[t]+1,q.push(t+1); if(2*t<=100000&&a[2*t]==0) a[t*2]=a[t]+1,q.push(t*2); q.pop(); } return a[k];}int main(){ int n,k; cin>>n>>k; cout<<bfs(n,k);}
- poj 3278 -- Catch That Cow (最短路)
- Catch That Cow POJ - 3278 [bfs][最短路]
- Catch That Cow 2007年USACO(最短路)
- POJ3278 Catch That Cow(最短路+bfs)
- poj 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS)
- POJ--3278:Catch That Cow (BFS)
- POJ 3278 - Catch That Cow(BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ - 3278 - Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278-Catch That Cow(bfs)
- POJ 3278(Catch That Cow)
- POJ-3278-Catch That Cow(BFS)
- POJ 3278Catch That Cow(BFS)
- POJ 3278 Catch That Cow(BFS)
- poj 1125 -- Stockbroker Grapevine (最短路)
- HIVE Row_Sequence 实现列自增长
- 快速上手制作Icon Font
- poj 3083 -- Children of the Candy Corn (走迷宫)
- 2013年一二月份寒假SSH项目——Hibernate常见异常
- poj 3278 -- Catch That Cow (最短路)
- poj 2323 -- PERMS (dp)
- delphi技术专题---获取网卡物理地址之TLanaENum结构步骤学习分享
- HTTP header头文件标头
- ACE自适配通信环境中文技术文档(中篇:ACE程序员教程)Reactor实例
- [STL基础]Iterator迭代器
- poj 3176 -- Cow Bowling (dp)
- 几种任务调度的 Java 实现方法与比较
- 移动游戏 请别迷信“一夜之间”