题意:

0到N的数轴上,每次可以选择移动到x-1,x+1,2*x,问从n移动到k的最少步数。

思路:

同时遍历三种可能并记忆化入队即可。

Tips:

n大于等于k时最短步数为n-k。

在移动的过程中可能会越界、重复访问。

poj不支持<bits/stdc++.h>和基于范围的for循环。

#include <iostream>
#include <queue>
using namespace std; const int M=110000; struct P{int x,step;}; int n,k,vis[M]; void bfs(){
queue<P> q;
q.push(P{n,0});
vis[n]=1;
while(!q.empty()){
P now=q.front();
q.pop();
int x[3]={now.x-1,now.x+1,2*now.x};
for(int i=0;i<3;i++){
if(x[i]<0||x[i]>=M){
continue;
}
else if(x[i]==k){
cout<<now.step+1;
return;
}
else if(vis[x[i]]==0){
q.push(P{x[i],now.step+1});
vis[x[i]]=1;
}
}
}
} int main()
{
cin>>n>>k;
if(n>=k){
cout<<n-k;
}else{
bfs();
}
return 0;
}

poj 3278 Catch That Cow(记忆化广度优先搜索)的更多相关文章

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  3. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  4. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  7. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  8. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  10. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

随机推荐

  1. 转载 - Ubuntu源改国内源 与 批量更改ubuntu机器apt源

    change_apt_source.sh # !/bin/bash # 备份原来的源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak # 获取 ...

  2. /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh: line 19: mysql: command not found

    [root@test ~]# tail -f /tmp/zabbix_agentd.log /var/lib/zabbix/percona/scripts/get_mysql_stats_wrappe ...

  3. innobackupex: Connecting to MySQL server with DSN 'dbi:mysql

    [root@ma src]# innobackupex --user=root /root/backup --no-timestamp InnoDB Backup Utility v1.5.1-xtr ...

  4. AgileConfig - RESTful API 介绍

    AgileConfig AgileConfig是一个基于.net core开发的轻量级配置中心. AgileConfig秉承轻量化的特点,部署简单.配置简单.使用简单.学习简单,它只提取了必要的一些功 ...

  5. 【Docker】/usr/bin/docker-current: Cannot connect to the Docker daemon at unix

    ------------------------------------------------------------------------------------------------- | ...

  6. logging模块简单用法

    logging模块功能比较多,但一般情况下使用其简单功能就已经足够了. 最简单的用法如下: import logging logging.baiscConfig(level=logging.DEBUG ...

  7. python异步回调顺序?是否加锁?

    话不多说,直接上代码: import time from functools import partial from concurrent.futures.process import Process ...

  8. 痞子衡嵌入式:MCUBootFlasher v3.0发布,为真实的产线操作场景而生

    -- 痞子衡维护的NXP-MCUBootFlasher工具(以前叫RT-Flash)距离上一个版本(v2.0.0)发布过去一年半以上了,这一次痞子衡为大家带来了全新版本v3.0.0,从这个版本开始,N ...

  9. springmvc 字符串转日期格式

    http://www.mamicode.com/info-detail-2485490.html

  10. USB限流芯片,4.8A最大,过压关闭6V

    PW1503,PW1502是超低RDS(ON)开关,具有可编程的电流限制,以保护电源源于过电流和短路保护.它具有超温保护以及反向闭锁功能. PW1503,PW1502采用薄型(1毫米)5针薄型SOT2 ...