POJ3278-Catch That Cow
来源:互联网 发布:老炮儿网络剧全集 编辑:程序博客网 时间:2024/06/06 07:04
典型的宽搜入门题。
注意边界条件。
#include <cstdio>#include <queue>#include <utility>using namespace std;const int MAX_N = 100000;typedef pair<int, int> P;queue<P> q;bool vis[MAX_N+1];int Bfs(int n, int k){ bool find_ans = false; q.push(P(n, 0)); P cur; while (!q.empty()) { cur = q.front(); q.pop(); if (cur.first == k) { find_ans = true; break; } if (cur.first + 1 >= 0 && cur.first + 1 <= MAX_N && vis[cur.first+1] == false) { q.push(P(cur.first+1, cur.second+1)); vis[cur.first+1] = true; } if (cur.first - 1 >= 0 && cur.first - 1 <= MAX_N && vis[cur.first-1] == false) { q.push(P(cur.first-1, cur.second+1)); vis[cur.first-1] = true; } if (cur.first * 2 >= 0 && cur.first * 2 <= MAX_N && vis[cur.first*2] == false) { q.push(P(cur.first*2, cur.second+1)); vis[cur.first*2] = true; } } if (find_ans) { return cur.second; } else return -1;}int main(){ int n, k; scanf("%d%d", &n, &k); printf("%d\n", Bfs(n, k)); return 0;}
0 0
- POJ3278 Catch That Cow
- poj3278 Catch That Cow
- poj3278 - Catch That Cow
- POJ3278 Catch That Cow
- POJ3278--Catch That Cow
- POJ3278 Catch That Cow
- poj3278 Catch That Cow
- poj3278 Catch That Cow
- poj3278--Catch That Cow
- POJ3278 Catch That Cow
- poj3278 Catch That Cow
- POJ3278,Catch That Cow
- poj3278--Catch That Cow
- POJ3278---Catch That Cow
- poj3278 Catch That Cow
- poj3278 catch that cow
- Catch That Cow poj3278
- Catch That Cow--poj3278
- Shiro学习(9)JSP标签
- 解决在onCreate()中AnimationDrawable只播放第一张图片的问题
- 魅族干货!如何从视觉设计师的角度做竞品分析?
- LeetCode Valid Parentheses
- Caffe深度学习框架上手教程
- POJ3278-Catch That Cow
- 树、森林、二叉树的转换
- Python re模块
- jQuery插件开发精品教程,让你的jQuery提升一个台阶
- js长轮询与短轮询的简单理解
- Android官方开发文档Training系列课程中文版:分享文件之配置文件共享
- 螺旋矩阵和蛇形矩阵
- 深度XP完美精简版 (适合低配置电脑)——迅雷
- android多媒体开发笔记