POJ 3278 Catch that cow
2015-08-08 16:21
337 查看
题目点我
简单的bfs,但是要剪枝。结果在剪枝数字的问题上RE了一万次,腿都软了。
简单的bfs,但是要剪枝。结果在剪枝数字的问题上RE了一万次,腿都软了。
#include <cstdio> #include <stdlib.h> #include <string.h> #include <queue> #include <vector> using namespace std; #define maxn 200005 int step[maxn]; char vis[maxn]; int bfs(int N, int K){ if(N == K) return 0; memset(vis, 'w', sizeof(vis)); queue<int> Q; Q.push(N); step = 0; vis = 'g'; while(!Q.empty()){ int x = Q.front(); int now; Q.pop(); vis[x] = 'b'; if(vis[x - 1] == 'w' && (x - 1 >= 0)){ if(x - 1 == K) return step[x] + 1; now = x - 1; step[now] = step[x] + 1; Q.push(now); vis[now] = 'g'; } if(vis[x + 1] == 'w' && (x + 1 <= maxn/2)){ if(x + 1 == K) return step[x] + 1; now = x + 1; step[now] = step[x] + 1; Q.push(now); vis[now] = 'g'; } if(vis[x * 2] == 'w' && (x * 2 <= maxn/2)){ if(x * 2 == K) return step[x] + 1; now = x * 2; step[now] = step[x] + 1; Q.push(now); vis[now] = 'g'; } } return -1; } int main(){ int N, K; while(scanf("%d %d", &N, &K) != EOF){ printf("%d\n", bfs(N, K)); } return 0; }
相关文章推荐
- hdu 1253 胜利大逃亡
- 干净清爽界面---资源浏览器形式的TXT阅读软件
- UVA 1423 Guess 【拓扑排序】
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart
- 自学perl 系列1
- 二分回顾
- 【Obective-C编程开发】Objective-C之BOOL一清二楚
- C语言中关于.h文件和.c文件详细解析
- C.Stripe(Codeforces Beta Round #18 (Div. 2 Only))
- 用户'sa'登录失败(DataGridView)
- MySQL连接查询
- 部分华为校园招聘机试试题及答案(C语言版本)
- 文件描述符及其重定向的学习笔记
- Input设备驱动之事件类型
- 设计模式--组合模式
- NSDatepicker
- iOS程序支持64位以及project.pbxproj文件的介绍
- socket长连接所用到的八大技术
- Thinkphp 3.2命名空间版如何使用PHPExcel导入Excel文件
- POJ3278.Catch That Cow