3278 & [kuangbin带你飞]专题一 简单搜索 C
来源:互联网 发布:g92车锥螺纹编程实例 编辑:程序博客网 时间:2024/06/05 17:58
C - Catch That Cow
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,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
- Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
- Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int maxn=1e5+5;int ans[maxn];int l,k;bool book[maxn];struct Node { int x,num; friend bool operator < (Node a,Node b) { return a.num>b.num; }};bool check(int num) { if(num>=0&&num<=100000) return true; return false;}int bfs() { Node now,next; priority_queue<Node>Q; while(!Q.empty()) Q.pop(); now.x=l,now.num=0; book[now.x]=false; Q.push(now); while(!Q.empty()) { now=Q.top(); Q.pop(); if(now.x==k) return now.num; next.x=now.x+1; if(check(next.x)&&book[next.x]) { book[next.x]=false; next.num=now.num+1; Q.push(next); } next.x-=1; next.x=now.x-1; if(check(next.x)&&book[next.x]) { book[next.x]=false; next.num=now.num+1; Q.push(next); } next.x+=1; next.x=now.x*2; if(check(next.x)&&book[next.x]) { book[next.x]=false; next.num=now.num+1; Q.push(next); } next.x/=2; } return 0;}int main(){ while(cin>>l>>k) { memset(ans,0,sizeof(ans)); memset(book,true,sizeof(book)); cout<<bfs()<<endl; }}
阅读全文
0 0
- 3278 & [kuangbin带你飞]专题一 简单搜索 C
- [kuangbin带你飞]专题一 简单搜索 C POJ3278
- [kuangbin带你飞]专题一 简单搜索 C
- [kuangbin带你飞]专题一 简单搜索-C
- [kuangbin带你飞]专题一 简单搜索 C
- [kuangbin带你飞]专题一 简单搜索C - Catch That Cow(POJ 3278)
- [kuangbin带你飞]专题一 简单搜索 C - Catch That Cow poj 3278
- [kuangbin神带你飞]专题一 简单搜索
- kuangbin带你飞 专题一 简单搜索 (题解)
- Fire Game [kuangbin带你飞]专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 总结
- Kuangbin带你飞专题一 简单搜索
- [kuangbin带你飞]专题一 简单搜索 D - Fliptile
- [kuangbin带你飞]专题一 简单搜索-J - Fire!
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- [kuangbin带你飞]专题一 简单搜索 A POJ1321
- [kuangbin带你飞]专题一 简单搜索 B POJ2251
- [kuangbin带你飞]专题一 简单搜索 D poj3279
- Windows 常用软件备忘
- 每日vim系列0716
- POJ 1007-DNA Sorting
- 如何优雅的记录编程中遇到的问题
- Different types of SVM -- opencv
- 3278 & [kuangbin带你飞]专题一 简单搜索 C
- spring
- Scheduling Tasks定时任务
- POJ 1008--Maya Calendar
- 【面试题】C++综合面试题
- iOS中 new Date() 返回 NaN
- express入门(7)- mysql
- 软件开发流程简介
- DC-7:指针(1)