hdoj 2717 Catch That Cow【bfs】
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9276 Accepted Submission(s):
2907
fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤
100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the
same number line. Farmer John has two modes of transportation: walking and
teleporting.
* Walking: FJ can move from any point X to the points X - 1
or X + 1 in a single minute
* Teleporting: FJ can move from any point X to
the point 2 × X in a single minute.
If the cow, unaware of its pursuit,
does not move at all, how long does it take for Farmer John to retrieve
it?
for Farmer John to catch the fugitive cow.
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 1000100
using namespace std;
int n,m;
int vis[MAX];
struct node
{
int x;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
int judge(int x)
{
if(x < 0||x > MAX||vis[x])
return 0;
return 1;
}
void bfs(int n,int k)
{
int i,j;
priority_queue<node>q;
node beg,end;
beg.x=n;
beg.step=0;
q.push(beg);
vis[n]=1;
while(!q.empty())
{
beg=q.top();
q.pop();
if(beg.x==k)
{
printf("%d\n",beg.step);
return ;
}
end.x=beg.x-1;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
end.x=beg.x+1;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
end.x=beg.x*2;
if(judge(end.x))
{
vis[end.x]=1;
end.step=beg.step+1;
q.push(end);
}
}
}
int main() {
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,0,sizeof(vis));
bfs(n,m);
}
return 0;
}
hdoj 2717 Catch That Cow【bfs】的更多相关文章
- POJ - 3278 Catch That Cow 【BFS】
题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...
- POJ 3278 Catch That Cow【BFS】
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Hdoj 2717.Catch That Cow 题解
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- Java之循环输出等腰三角形
public class aaa{ public static void main(String[] args) { int max=5; for(int i=1;i<=5;i++){//控制行 ...
- ACM HDU Primes(素数判断)
Problem Description Writea program to read in a list of integers and determine whether or not eachnu ...
- 从Bash漏洞学Shell脚本(冒号)
前天,爆发了Bash安全漏洞,非常恐怖.在网络上开始飞速传播,附带了非常友好的检测工具. $ env x='() { :;}; echo vulnerable' bash -c "echo ...
- 代码世界中的Lambda
“ λ ”像一个双手插兜儿,独自行走的人,有“失意.无奈.孤独”的感觉.λ 读作Lambda,是物理上的波长符号,放射学的衰变常数,线性代数中的特征值……在程序和代码的世界里,它代表了函数表达式,系统 ...
- ubuntu npm 私有库搭建 (npmjs.org 官方版本)
目标 npm.xxx.com 安装和推送nodejs包 npmui.xxx.com 管理已经推送的nodejs包 安装 couchdb https://launchpad.net/~couch ...
- JVM原理
Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行,而JVM是java的核心和基础,在ja ...
- svn修改密码跟账户
在co的时候直接输入账户跟密码 svn co ${SVNURL} ./ --username **--password *** 或者删除缓存文件,在Linux上面 删除~/.subversion/a ...
- 各浏览器Cookie大小、个数限制
一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...
- go 语言与循环
package main import "fmt" type Employee struct{name string; age int} func displayName(e *E ...
- wndows 7 Wifi热点
2016年09月13日 14時52分 wanglinqiang整理 Step1 cmd.exe(管理员身份运行) Step2 命令行输入[netsh wlan set hostednetwork mo ...