[POJ 3278]Catch That Cow[BFS]
来源:互联网 发布:国产三维制衣软件 编辑:程序博客网 时间:2024/06/11 20:11
题目链接:[POJ 3278]Catch That Cow[BFS]
题意分析:
农夫每次可以向左或者向右走,或者跳跃到当前位置的两倍位置上。问:最少几次操作可以到达奶牛的K位置
解题思路:
从起点BFS即可。注意边界条件和访问标记量。
个人感受:
虽然简单,还是ME+WA。ME是因为忘记标记访问量,WA是因为边界上的2*X<=100000写成了<。可见再简单也得细心啊
具体代码如下:
#include<algorithm>#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<iomanip>#include<iostream>#include<map>#include<queue>#include<set>#include<sstream>#include<stack>#include<string>#define pr(x) cout << #x << " = " << (x) << '\n';using namespace std;struct P { int x, dis; P(int a, int b):x(a),dis(b){}};bool vis[100100];int main(){ int n, k; cin >> n >> k; queue<P> q; q.push(P(n, 0)); while (q.size()) { P cur = q.front(); q.pop(); if (cur.x == k) { printf("%d\n", cur.dis); break; } if (cur.x > 0 && !vis[cur.x - 1]) { vis[cur.x - 1] = 1; q.push(P(cur.x - 1, cur.dis + 1)); } if (cur.x < 100000 && !vis[cur.x + 1]) { vis[cur.x + 1] = 1; q.push(P(cur.x + 1, cur.dis + 1)); } if (cur.x * 2 <= 100000 && !vis[cur.x * 2]) { vis[cur.x * 2] = 1; q.push(P(cur.x * 2, cur.dis + 1)); } } return 0;}
0 0
- poj 3278 Catch That Cow BFS
- poj 3278 Catch That Cow BFS
- POJ 3278 Catch That Cow BFS
- poj 3278 bfs(Catch That Cow)
- poj 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow bfs
- POJ 3278 Catch That Cow 【bfs+队列】
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS)
- poj 3278 Catch That Cow bfs
- poj 3278 Catch That Cow (BFS剪枝)
- POJ 3278 Catch That Cow[BFS]
- Poj 3278 Catch That Cow 裸bfs
- POJ 3278 Catch That Cow(图论:BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow 【BFS】
- POJ--3278:Catch That Cow (BFS)
- 224. Basic Calculator
- 用1,2,3…9组成3个三位数abc, def, ghi, 每个数字恰好使用一次,且abc:def:ghi=1:2:3,输出所有解。 用1,2,3…9组成3个三位数abc, def, ghi, 每个数
- PB中调用存储过程获取返回值
- LeetCode 19 Remove Nth Node From End of List
- Linux Shell 管道命令(pipe)的使用
- [POJ 3278]Catch That Cow[BFS]
- Uiautomator 2.0之UiWatcher类学习小记
- QT12 How to open a new window after successful Login
- MOEA/D算法的自己的一些理解
- HTTP/HTTPS抓包工具Charles
- Linux---centos安装配置并挂载NFS
- Scrum 简介
- 怎样创建一个xcode插件 第2部分/3部分
- LinkedList源码解析