POJ 3278.Catch That Cow
来源:互联网 发布:英文随机名字生成算法 编辑:程序博客网 时间:2024/05/05 11:44
简单BFS,之前碰见过,但是因为当时太弱了没搞出来。。。(虽然现在也很弱 - -)
使用了quene,然而脑残忘了清空队列,wa了好几发
#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <algorithm>using namespace std;queue<int> p;bool vis[100010];int ans[100010];int bfs(int n, int k){ int head, tail; memset(vis, false, sizeof(vis)); memset(ans, 0, sizeof(ans)); vis[n] = true; //ans[n] = 0; p.push(n); while(!p.empty()){ head = p.front(); p.pop(); for(int i = 0; i < 3; i++){ if(!i) tail = head + 1; else if(i == 1) tail = head - 1; else tail = head * 2; if(!vis[tail] && tail < 100010 && tail >= 0){ p.push(tail); ans[tail] = ans[head] + 1; vis[tail] = true; } if(tail == k) return ans[tail]; } } return 0;}int main(){ int n, k; while(~scanf("%d%d", &n, &k)){ while(!p.empty()) p.pop(); printf("%d\n", bfs(n ,k)); } return 0;}
0 0
- POJ 3278.Catch That Cow
- POJ 3278.Catch That Cow
- Catch That Cow(POJ--
- (poj)Catch That Cow
- Catch That Cow POJ
- Catch That Cow POJ
- Catch That Cow POJ
- Catch That Cow POJ
- Catch That Cow POJ
- Catch That Cow POJ
- 3278. Catch That Cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- poj 3278 catch that cow
- poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- Poj 3278 Catch That Cow
- POJ 3278 Catch That Cow
- ffmpeg 如何音视频同步
- FFmpeg音视频同步的问题
- 音视频同步之时间戳
- JSON使用JsonPropertyAttribute
- 程序员代码面试指南:IT名企算法与数据结构题目最优解(题目)
- POJ 3278.Catch That Cow
- 根据数据动态显示列及数据的SQL语句
- Android短信发送流程之多收件人发送(原)
- 05Redis入门指南笔记(持久化)
- 【Leetcode】3Sum 【wrong】
- Codeforces 335A.Magic Spheres
- du-查看文件夹大小-并按大小进行排序
- 创业狗们为什么要向《芈月传》里的张仪看齐?
- UI中的自适应高度