一个简单的bfs题。

用结构体的目的在于保存bfs到达此处时走的步数。

不多言,上AC代码:

//18:18
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
int step[]={-,};
const int maxn=;
int flag[maxn];
struct state{
int cur;
int cnt;
};
queue<state> qs;
int bfs(){
state src,dst;
src.cnt=;src.cur=n;
qs.push(src);
src.cnt=;src.cur=n;
while(qs.empty()==){
state now=qs.front();
qs.pop();
//printf("%d %d\n",now.cur,now.cnt);
if(now.cur==k)return now.cnt;
state next;
next.cnt=now.cnt+;
flag[now.cur]=;
for(int i=;i<;i++){
next.cur=now.cur+step[i];
if(next.cur<||next.cur>maxn)continue;
if(flag[next.cur]==){
qs.push(next);
}
}
next.cur=now.cur*;
if(next.cur<||next.cur>maxn)continue;
if(flag[next.cur]==){
qs.push(next);
}
}
return -;
}
int main(void){
scanf("%d%d",&n,&k);
memset(flag,,sizeof(flag));
printf("%d\n",bfs());
return ;
}

POJ3278_Catch that cow的更多相关文章

  1. POJ3278_Catch That Cow(JAVA语言)

    思路:bfs裸题.三个选择:向左一个单位,向右一个单位,向右到2*x //注意,需要特判n是否大于k,大于k时只能向左,输出n-k.第一次提交没注意,结果RE了,, Catch That Cow Ti ...

  2. POJ 3278 Catch That Cow(bfs)

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

  3. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  6. 细读cow.osg

    细读cow.osg 转自:http://www.cnblogs.com/mumuliang/archive/2010/06/03/1873543.html 对,就是那只著名的奶牛. //Group节点 ...

  7. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  8. raw,cow,qcow,qcow2镜像的比较

    在linux下,虚拟机的选择方式有很多,比如vmware for linux,virtual box,还有qemu,在以前,使用qemu的人不多,主要是使用起来有些麻烦,但现在随着Openstack的 ...

  9. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

随机推荐

  1. AndroidStudio build.gradle 报错

    Android Studio. I'm getting this kind of error during application run. Error:Execution failed for ta ...

  2. 删除 Linux /tmp 目录下的临时文件

    使用 tmpreaper How to install and use tmpreaper How to Remove Files Older than N days using tmpreaper ...

  3. nginx软件的编译安装步骤

    1.1 检查软件安装的系统环境 [root@web02 conf]# cat /etc/redhat-release CentOS release 6.8 (Final) [root@web02 co ...

  4. 利用Python进行端口扫描

    利用Python进行端口扫描 - Dahlhin - 博客园 https://www.cnblogs.com/dachenzi/p/8676104.html Python实现对一个网络段扫描及端口扫描 ...

  5. SpringCloud 进阶之分布式配置中心(SpringCloud Config)

    1. SpringCloud Config SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用 的所有环境提供了一个中心化的外部配置; ...

  6. format的用法:python

    https://www.cnblogs.com/wongbingming/p/6848701.html 它通过{}和:来代替%.通过位置 In [1]: '{0},{1}'.format('kzc', ...

  7. mysql 约束条件 外键 forigen key 介绍

    外键 forigen key作用 :建立表之间的关系 什么是外键 员工信息表有这些字段:id号  姓名 性别 员工所在部门名 部门描述信息 公司有3个部门,但是有1个亿的员工,那意味着 员工所对应的部 ...

  8. centos 文档的压缩和打包 gzip,bzip2,xz,zip,unzip,tar,tgz 第九节课

    centos  文档的压缩和打包   gzip,bzip2,xz,zip,unzip,tar,tgz  第九节课 SAS盘可以支持热插拔,看机器 tar.zip.tar -czvf 不会动源文件,gz ...

  9. blockchain 区块链的开发,基于python或node js

    现在很多人用node js做区块链的开发,因为点对点并发是区块链中的难点技术之一,而node js天然的对并发支持比较好,因此比较有优势. http://ecomunsing.com/build-yo ...

  10. google浏览器插件安装

    1:安装本地插件,直接将下载好的crx插件拖入到   chrome://extensions/    的空白处 http://www.cnplugins.com/tool/outline-instal ...