(广搜)Catch That Cow -- poj -- 3278
链接:
http://poj.org/problem?id=3278
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 62113 | Accepted: 19441 |
Description
Farmer John has been informed of the location of a 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?
Input
Output
Sample Input
- 5 17
Sample Output
- 4
Hint
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- #include <queue>
- using namespace std;
- #define N 110000
- struct node
- {
- int x, step;
- };
- int s, e;
- bool vis[N];
- int BFS(int s)
- {
- node p, q;
- p.x = s, p.step = ;
- memset(vis, false, sizeof(vis));
- vis[s] = true;
- queue<node>Q;
- Q.push(p);
- while(Q.size())
- {
- p = Q.front(), Q.pop();
- if(p.x == e) return p.step;
- for(int i=; i<; i++)
- {
- if(i==)
- q.x = p.x + ;
- else if(i==)
- q.x = p.x - ;
- else if(i==)
- q.x = p.x * ;
- q.step = p.step + ;
- if(q.x>= && q.x<N && !vis[q.x])
- {
- Q.push(q);
- vis[q.x] = true;
- }
- }
- }
- return -;
- }
- int main()
- {
- while(scanf("%d%d", &s, &e)!=EOF)
- {
- int ans = BFS(s);
- printf("%d\n", ans);
- }
- return ;
- }
(广搜)Catch That Cow -- poj -- 3278的更多相关文章
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- C - Catch That Cow POJ - 3278
//标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
- kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
随机推荐
- centos6.6 myphpadmin
基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...
- linux Posix 信号量 三 (经典例子)
本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...
- 马士兵Spring-AOP-XML配置(2)
一. UserDAO.java: package com.cy.dao; import com.cy.model.User; public interface UserDAO { public voi ...
- jQuery的文档操作
1.插入操作 一.父元素.append(子元素) 追加某元素 父元素中添加新的元素 var oli = document.createElement('li'); oli.innerHTML = '哈 ...
- 文件os.path相关方法
#!/usr/bin/python3# -*- coding: utf-8 -*-# @Time : 2018/6/13 15:03# @File : abspath_1.py impor ...
- 分布式锁实践(一)-Redis编程实现总结
写在最前面 我在之前总结幂等性的时候,写过一种分布式锁的实现,可惜当时没有真正应用过,着实的心虚啊.正好这段时间对这部分实践了一下,也算是对之前填坑了. 分布式锁按照网上的结论,大致分为三种:1.数据 ...
- 225. Implement Stack using Queues + 232. Implement Queue using Stacks
▶ 栈和队列的相互表示.发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小. ▶ 第 225 题,用队列实现栈 ● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出 ...
- 如何优化Java垃圾回收-zz
为什么需要优化GC 或者说的更确切一些,对于基于Java的服务,是否有必要优化GC?应该说,对于所有的基于Java的服务,并不总是需要进行GC优化,但前提是所运行的基于Java的系统,包含了如下参数或 ...
- MVC4 AspNet MVC下的Ajax / 使用微软提供的Ajax请求脚本 [jquery.unobtrusive-ajax.min.js]
源码参考:链接:http://pan.baidu.com/s/1pKhHHMj 密码:mkr4 1:新建-->项目-->Web-->ASP.NET MVC 4 Web 应用程序.命 ...
- 探究算子find_shape_model中参数MaxOverlap的准确意思
基于形状的模板查找算子: find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxO ...