【poj 3278】Catch That Cow 题意&题解&代码(C++)
来源:互联网 发布:js object 添加属性 编辑:程序博客网 时间:2024/04/29 03:13
题目链接:
http://poj.org/problem?id=3278
题意:
给定两个整数n和k
通过 n+1或n-1 或n*2 这3种操作,使得n==k
输出最少的操作次数
题解:
bfs搜索,注意搜索途中的判断,小心会多做一些无用操作,小心re。。
代码:
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<queue>using namespace std;int n,k,dis[200005];queue<int>q;int main(){ scanf("%d%d",&n,&k); q.push(n); memset(dis,-1,sizeof(dis)); int flag=0; dis[n]=0; while(!q.empty()&&flag==0) { int now=q.front();q.pop(); if (now<=k && dis[now+1]==-1) { dis[now+1]=dis[now]+1; if (now+1==k) flag=1; q.push(now+1); } if (now-1>=0 && dis[now-1]==-1) { dis[now-1]=dis[now]+1; if (now-1==k) flag=1; q.push(now-1); } if (now*2<=k*2 && dis[now*2]==-1) { dis[now*2]=dis[now]+1; if (now*2==k) flag=1; q.push(now*2); } } printf("%d\n",dis[k]);}
0 0
- 【poj 3278】Catch That Cow 题意&题解&代码(C++)
- 【poj 3267】The Cow Lexicon 题意&题解&代码(C++)
- 【poj 3176】Cow Bowling 题意&题解&代码(C++)
- POJ 3278 Catch That Cow【C语言版】
- 【poj 1703】Find them, Catch them 题意&题解&代码(C++)
- 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)
- POJ-3278-Catch That Cow(BFS)
- POJ 3278Catch That Cow(BFS)
- 主存与Cache的地址映射[转载]
- 常用socket函数详解
- c++中const小结
- hdu 4549 矩阵快速幂+费马小定理
- 1263 末日传说
- 【poj 3278】Catch That Cow 题意&题解&代码(C++)
- ceph学习之路1:虚拟机安装ceph
- 输入adb shell 时 提示error: more than one device and emulator
- 【spark+nlp】 Feature Extract and Preprocess
- FZU 1001
- TUN(IP Tunneling)介绍
- 【杭电】[4505]小Q系列故事——电梯里的爱情
- codeforces_612C.Replace To Make Regular Bracket Sequence(stack)
- CF 343div2 B 暴力