HDU 2717 Catch That Cow(BFS,每次有3种走路方式,问最少的步数到达目的地)
2015-11-01 18:21
417 查看
题目地址:点击打开链接
题意:农民要在一条线捉一头牛,假设他的坐标为x,则他有3种走路方式(1)x-1 (2)x+1 (3)2*x;问最少的步数捉到牛
思路:刚上来直接模拟也没判断,结果果断超内存,减了不少还是超,看了别人的题解,我是出来的时候判断结果T了,别人是进去的时候判断结果A了,有时间仔细研究一下,还有一个神减枝是应经走过的路标记一下,下一次走过的时候时间肯定比第一次走过的时间长,所以可以直接减掉
AC代码:
MLE代码1:
这个貌似有点太浪了
MLE代码2:
题意:农民要在一条线捉一头牛,假设他的坐标为x,则他有3种走路方式(1)x-1 (2)x+1 (3)2*x;问最少的步数捉到牛
思路:刚上来直接模拟也没判断,结果果断超内存,减了不少还是超,看了别人的题解,我是出来的时候判断结果T了,别人是进去的时候判断结果A了,有时间仔细研究一下,还有一个神减枝是应经走过的路标记一下,下一次走过的时候时间肯定比第一次走过的时间长,所以可以直接减掉
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; int n,k; int visit[100010]; struct node { int a; int step; }from,temp; bool check(int x) { if(x < 0 || x > 100000 || visit[x]) return false; return true; } void bfs() { queue<node> q; from.a = n; from.step = 0; q.push(from); while(!q.empty()) { temp = q.front(); q.pop(); if(temp.a == k) break; from.a = 2 * temp.a; if(check(from.a)) { visit[from.a] = 1; from.step = temp.step + 1; q.push(from); } from.a = temp.a - 1; if(check(from.a)) { visit[from.a] = 1; from.step = temp.step + 1; q.push(from); } from.a = temp.a + 1; if(check(from.a)) { visit[from.a] = 1; from.step = temp.step + 1; q.push(from); } } printf("%d\n",temp.step); } int main() { while(scanf("%d%d",&n,&k) != EOF) { memset(visit,0,sizeof(visit)); bfs(); } return 0; }
MLE代码1:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; int n,k; struct node { int a; int step; }from,temp; void bfs() { queue<node> q; from.a = n; from.step = 0; q.push(from); while(!q.empty()) { temp = q.front(); q.pop(); if(temp.a == k) break; from.a = 2 * temp.a; from.step = temp.step + 1; q.push(from); from.a = temp.a - 1; from.step = temp.step + 1; q.push(from); from.a = temp.a + 1; from.step = temp.step + 1; q.push(from); } printf("%d\n",temp.step); } int main() { while(scanf("%d%d",&n,&k) != EOF) { bfs(); } return 0; }
这个貌似有点太浪了
MLE代码2:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; int n,k; int visit[100010]; struct node { int a; int step; }from,temp; bool check(int x) { if(x < 0 || x > 100000 || visit[x]) return false; return true; } void bfs() { queue<node> q; from.a = n; from.step = 0; q.push(from); while(!q.empty()) { temp = q.front(); q.pop(); if(temp.a == k) break; if(check(temp.a)) { if(temp.a < k) { from.a = 2 * temp.a; from.step = temp.step + 1; q.push(from); from.a = temp.a + 1; from.step = temp.step + 1; q.push(from); } if(temp.a > k / 2) { from.a = temp.a - 1; from.step = temp.step + 1; q.push(from); } } } printf("%d\n",temp.step); } int main() { while(scanf("%d%d",&n,&k) != EOF) { memset(visit,0,sizeof(visit)); bfs(); } return 0; }
相关文章推荐
- 就恢复计划
- ASP.NET之Gridview图解
- JAVA i++线程非安全测试1
- JAVA i++线程非安全测试1
- 分页插件开发(1)--jstl自定义标签
- 使用fat jar和proguard对包含第三方依赖包的java代码进行打包混淆
- springMVC使用注解操作HttpSession中的对象
- spring中AOP3
- Regular Expression Matching
- Django form 文件上传
- Eclipse创建Django项目
- (C++ )矩阵
- 太乱了的apache,我自己都无语了
- iOS--UIImageView的使用方法
- jquery.cookie() 方法的使用(读取、写入、删除)
- jquery.cookie() 方法的使用(读取、写入、删除)
- HDOJ 2803 The MAX【水题】
- java怎样杀死一个指定线程
- Xcode7开发iOS记录(一)
- 自组合(Self-Assembly,UVa 1572)