poj 3278 Catch That Cow bfs
2016-02-28 13:04
357 查看
题目
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105944#overview题目来源:2016寒假高年训练3
简要题意:+1 -1 *2三种操作问n最少多少步到m
题解
最基础的bfs,注意判断一些边界条件即可。使用三个方式去搜,复杂度为线性。
代码
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <queue> #include <string> #include <vector> #include <set> #include <map> #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define fi first #define se second using namespace std; typedef long long LL; typedef vector<int> VI; typedef pair<int,int> PII; // head const int N = 1e5+5; const int INF = 0x3f3f3f3f; int a ; int n, k; void insert(queue<int> &q, int x, int cur) { if (x > N || x < 0 || a[x] != INF) return; q.push(x); a[x] = a[cur]+1; } int main() { while (scanf("%d%d", &n, &k) == 2) { memset(a, INF, sizeof a); a = 0; queue<int> q; q.push(n); while (!q.empty()) { int cur = q.front(); q.pop(); if (cur == k) break; insert(q, cur+1, cur); insert(q, cur-1, cur); insert(q, cur<<1, cur); } printf("%d\n", a[k]); } return 0; }
相关文章推荐
- vfork Function
- [译]如何防止elasticsearch的脑裂问题
- Flatten Binary Tree to Linked List
- ubuntu安装python
- 【Codeforces Round 340 (Div 2)C】【暴力排序枚举】Watering Flowers 2个灌溉器灌溉所有点最小的rr+RR
- [效率神技]Intellij 的快捷键和效率技巧|系列一|常用快捷键
- 大型网站架构系列:负载均衡详解(2)
- Database Replay wrc命令报错ORA-15552: workload replay client cannot login to database server
- Manthan, Codefest 16 E. Startup Funding ST表 二分 数学
- 自定义dialog类
- genymotion无法下载解决方法
- 【Codeforces Round 340 (Div 2)B】【简单题】Chocolate 巧克力划分方案数 每段恰有一个坚果
- java重拍 https://www.zhihu.com/question/34387716/answer/76029594
- Uiautomator编译运行的自动化
- SDK无法更新
- andorid build tools 19.1
- LeetCode OJ 1. Two Sum
- mySql乱码
- visualSVN server库迁移
- 大型网站架构系列:负载均衡详解(1)