UVA 10120 - Gift?!(搜索+规律)
| Problem D. Gift?! |
The Problem
There is a beautiful river in a small village. N rocks are arranged in a straight line numbered 1 to N from left bank to the right bank, as shown below.
[Left Bank] - [Rock1] - [Rock2] - [Rock3] - [Rock4] ... [Rock n] - [Right Bank]
The distance between two adjacent rocks is exactly 1 meter, while the distance between the left bank and rock 1 and the distance between Rock n and the right bank are also 1 meter.
Frog Frank was about to cross the river, his neighbor Frog Funny came to him and said,
'Hello, Frank. Happy Children's Day! I have a gift for you. See it? A little parcel on Rock 5.'
'Oh, that's great! Thank you! I'll get it.'
'Wait...This present is for smart frogs only. You can't get it by jumping to it directly.'
'Oh? Then what should I do?'
'Jump more times. Your first jump must be from the left bank to Rock 1, then, jump as many times as you like - no matter forward or backward, but your ith jump must cover 2*i-1 meters. What's more, once you return to the left bank or reach the right bank, the game ends, and no more jumps are allowed.'
'Hmmm, not easy... let me have a think!' Answered Frog Frank, 'Should I give it a try?'
The Input
The input will contain no more than 2000 test cases. Each test case contains a single line. It contains two positive integers N (2<=N<=10^6), and M (2<=M<=N), M indicates the number of the rock on which the gift is located. A test case in which N=0, M=0 will terminate the input and should not be regarded as a test case.
The Output
For each test case, output a single line containing 'Let me try!' If it's possible to get to Rock m, otherwise, output a single line containing 'Don't make fun of me!'
Sample Input
9 5
12 2
0 0
Sample Output
Don't make fun of me!
Let me try!
Note
In test case 2, Frank can reach the gift in this way:
Forward(to rock 4), Forward(to rock 9), Backward(to rock 2, got the gift!)
Note that if Frank jumps forward in his last jump, he will land on the right bank(assume that banks are large enough) and thus, lost the game.
题意:n个石头,最终目标是m,每次移动1,3,5,7.步,步数递增,次数不限,只要不跳到石头之外就可以了,求是否能跳到m。
思路:n>=49的话,不管什么位置都可以跳到,<49的时候进行搜索。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; int n, m;
struct State {
int v, k;
}p; bool bfs() {
queue<State>Q;
p.v = 1; p.k = 3;
Q.push(p);
while (!Q.empty()) {
p = Q.front(); Q.pop();
if (p.v == m) return true;
State q = p; q.v += q.k; q.k += 2;
if (q.v > 0 && q.v <= n)
Q.push(q);
q = p; q.v -= q.k; q.k += 2;
if (q.v > 0 && q.v <= n)
Q.push(q);
}
return false;
} void solve() {
if (n <= 49 && !bfs()) printf("Don't make fun of me!\n");
else printf("Let me try!\n");
} int main() {
while (~scanf("%d%d", &n, &m) && n + m) {
solve();
}
return 0;
}
UVA 10120 - Gift?!(搜索+规律)的更多相关文章
- UVa 10120 - Gift?!
题目大意 美丽的村庄里有一条河,N个石头被放置在一条直线上,从左岸到右岸编号依次为1,2,...N.两个相邻的石头之间恰好是一米,左岸到第一个石头的距离也是一米,第N个石头到右岸同样是一米.礼物被放置 ...
- UVa 10285【搜索】
UVa 10285 哇,竟然没超时!看网上有人说是记忆化搜索,其实不太懂是啥...感觉我写的就是毫无优化的dfs暴力....... 建立一个坐标方向结构体数组,每个节点dfs()往下搜就好了. #in ...
- uva 10120
bfs搜索 当n大于等于49 是 总是可能的 ~ http://www.algorithmist.com/index.php/UVa_10120 #include <cstdio> #i ...
- UVa 11774 (置换 找规律) Doom's Day
我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m) 不过神题的题解还须神人写.. We can associate at each cell a base 3-numb ...
- uva 10994 - Simple Addition(规律)
题目链接:uva 10994 - Simple Addition 题目大意:给出l和r,求∑(l≤i≤r)F(i), F(i)函数题目中有. 解题思路:由两边向中间缩进,然后l和r之间的数可以按照1~ ...
- UVA 10471 Gift Exchanging
题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用 ...
- 紫书 例题7-14 UVa 1602(搜索+STL+打表)
这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...
- 紫书 习题 8-20 UVa 1620 (找规律+求逆序对)
这道题看了半天没看出什么规律, 然后看到别人的博客, 结论是当n为奇数且逆序数为奇数的时候 无解, 否则有解.但是没有给出证明, 在网上也找到详细的证明--我也不知道是为什么-- 求逆序对有两种方法, ...
- 紫书 习题8-5 UVa 177 (找规律)
参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...
随机推荐
- Spring JDBC RowMapper接口示例
JdbcTemplate类使用org.springframework.jdbc.core.RowMapper <T>接口在每行的基础上映射ResultSet的行.该接口的实现执行将每行映射 ...
- Python——eventlet.hubs
Hub构成了 Eventlet 的事件循环,它分发 I/O 事件.调度 greenthread.Hub的存在使得协程被提升为 greenthreads. Eventlet 有多种hub的实现,所以在使 ...
- WAS集群:记一次Node Agent不活动问题解决过程
之前很少接触集群,准确地说是很少接触项目现场的实施工作,或者说接触到的都是比较简单的实施工作,安装Linux.WAS.Oracle相对来说都比较简单.一直埋头干着研发的活,干着不要紧,一干就是好几年. ...
- Sublime text 3 中Package Control 的安装与使用方法
Package Control插件本身是一个为了方便管理插件的插件,在Sublime text 3中,Package Control 的安装方法一开始出来的方法是要先安装Git, 再输入代码来安装,原 ...
- YII2常用数据库操作
//1.简单查询 $admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->findAll(&q ...
- Markdown编辑器Editor.md使用方式
摘要: 搭建个人博客时,涉及文章上传,文章展示,这里需要一个Markdown插件,mark一下. Editormd下载地址:http://pandao.github.io/editor.md/ 由于前 ...
- es 5 数组reduce方法记忆
reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值. 概念:对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并 ...
- 【2018年12月14日】A股最便宜的股票
新钢股份(SH600782) - 当前便宜指数:193.12 - 滚动扣非市盈率PE:2.91 - 动态市净率PB:0.96 - 动态年化股息收益率:1.75% - 新钢股份(SH600782)的历史 ...
- 错误 Unable to find vcvarsall.bat 的终极无敌最完美的解决办法
Windows 上通过 pip 安装 python 包,经常会出现这种错误. 如:pip install pyodbc. 这种错误的简单明了解释就是:python 编译器找不到计算机上面的 VC 编译 ...
- 关于解决emoji表情的存储
近段时间处理,由于工作需求,需要使得用户插入的emoji表情能够正常显示及使用,所以做个总结,以备后用. 说明:本方法只在mysql环境中测试 1.首先程序在连接数据库时,要指定数据库字符集的设置 c ...