hdu_2717_Catch That Cow_bfs
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2717
题解:一维简单BFS,详细看代码,0ms。
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=;
bool v[maxn];
int n,k;
struct nd{
int x,step;
};
bool check(int x){
if(x<||x>||v[x])return false;
else return true;
}
int bfs(int s){
for(int i=;i<=;i++)v[i]=false;//初始化标记
queue<nd>Q;
nd S;
S.x=s,S.step=;
v[s]=;
Q.push(S);
while(!Q.empty()){
nd now=Q.front();Q.pop();
if(now.x==k)return now.step;
if(check(now.x+)){
nd tmp;tmp.x=now.x+,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
if(check(now.x-)){
nd tmp;tmp.x=now.x-,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
if(check(now.x*)){
nd tmp;tmp.x=now.x*,tmp.step=now.step+;
v[tmp.x]=;
Q.push(tmp);
}
}
return -;
}
int main(){
while(~scanf("%d%d",&n,&k)){
if(n>=k){printf("%d\n",n-k);continue;}//剪枝
int ans=bfs(n);
printf("%d\n",ans);
}
return ;
}
hdu_2717_Catch That Cow_bfs的更多相关文章
- HDU_2717_Catch That Cow
很短的 BFS 队列 HDU_2717_Catch That Cow #include<iostream> #include<algorithm> #include<cs ...
- Catch That Cow_bfs
Catch That Cow 题目大意:FrammerJohn找奶牛,给出n和k.FJ在n处.每次他可以向左移动一格.向右移动一格或者移动到自己当前格子数乘2的地方.求FJ最少移动多少次.其中,FJ和 ...
随机推荐
- @Transactional问题记录下
系统中多数据源 在control 层 分别使用不同数据源的service ,有个service 调用相应的dao直接出现表或视图不存在,发现这个service类上加了@Transactional 注 ...
- emacs 使用教程
http://www.cnblogs.com/liuchaogege/p/4464211.html
- subsequence/subsets/subarray/substring problems
128. Longest Consecutive Sequence hashmap, int up = nums[i], int down, int max 注:访问过的要erase 152. Max ...
- xml动态修改 dom4j修改
xml的动态修改需要传入的参数 xml的位置(tomcat中的发布位置).修改后的xml需要保存的位置(因为动态修改,所以建议和xml未修改前的位置相同).添加的节点的信息.或者修改的节点的信息 SA ...
- DHCP源码分析--主流程
DHCP 服务器,客户端代码都采用了统一的事件轮询(event loop),包含了任务处理消息,定时器消息,socke收发消息等等. static struct { isc_appmethods_t ...
- PHP strtotime在linux服务器时间延迟8小时问题
今天客户反映有个功能投票模块第一天投了后,第二天就不能投了,理论上是第二天凌晨就可以再答题的,发现本地是正常的,linux服务器异常, 仔细查找原因发现是strtotime函数获取的值和本地获取的值不 ...
- Redis 使用 Eval 多个键值自增操作示例
在PHP上使用Redis 给多个键值进行自增,示例如下: $set['money'] = $this->redis->hIncrByFloat($key, $hour .'_money', ...
- HDU 5880 Family View
$AC$自动机. 用$AC$自动机匹配一次,开一个$flag$记录一下以$i$位置为结尾的最长要打$*$多少个字符,然后倒着扫描一次就可以输出了. #pragma comment(linker, &q ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- C#Winform实现自动更新
服务端: [WebMethod] public string GetNewService(string version) { //通过版本号进行比较 if (version == "v1.0 ...