HDU 2717】的更多相关文章

HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先找到的一定是最小的步数. /* HDU 2717 Catch That Cow --- BFS */ #include <cstdio> #include <cstring> #include <queue> using namespace std; ]; int n, k…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been…
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. 然后还要注意N>=K的时候,只能减1才能到达. #include <iostream> #include <string> #include <cstdio> #include <cmath> #include <vector> #includ…
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20259    Accepted Submission(s): 5926 Problem Description Farmer John has been i…
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…
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; struct tt { int step,tem; }; ]; queue <tt> q; int bfs(tt s,tt e) { tt front,temp; q.push(s); visit[s.tem]=; while(!q.empty()) {…
简单的广搜: #include <cstdio> #include <queue> using namespace std; ],step[]; int n,start,end,res; bool check(int x) { )&&(x<)&&(map[x])) return true; else return false; } int bfs() { int temp; if(start==end) return res; queue<…
题意是在一个数轴上,每次可以一步到达当前位置数值的 2 倍的位置或者数值 +1 或数值 -1 的位置,给定 n 和 k,问从数值为 n 的位置最少多少步可以到达数值为 k 的位置. 用广搜的方法,把已经到达的位置标记,检查三个方向(*2,+1,-1)的位置是否到达 k,如已经到达就返回遍历的层数,否则将新的位置标记,继续检查新位置的三个方向. 这题要注意剪枝,每次的新位置要大于 0 小于 1e6,且当 n 大于 k 的时候要特殊处理,此时只能通过 -1 的方法到达 k,故输出 n - k 即可.…
在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点 Sample Input5 17 Sample Output4 #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; int n , k ; int ans ; ] ; struct node { int x ; int step ; }; bo…
Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6383    Accepted Submission(s): 2034 Problem Description Farmer John has been informed of the location of a fugitive cow and wants…