hdu 5523 Game 【BestCoder Round #61 (div.2)】
2015-11-01 12:09
225 查看
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 315 Accepted Submission(s): 126
Problem Description
XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each pillar has a jewel.Now XY is standing on the S-th pillar and the exit is in the T-th pillar.XY can leave from the exit only after they get all the jewels.Each time XY can move
to adjacent pillar,or he can jump to boundary ( the first pillar or the N-th pillar) by using his superpower.However,he needs to follow a rule:if he left the pillar,he no can not get here anymore.In order to save his power,XY wants to use the minimum number
of superpower to pass the game.
Input
There are multiple test cases, no more than 1000 cases.
For each case,the line contains three integers:N,S and T.(1≤N≤10000,1≤S,T≤N)
Output
The output of each case will be a single integer on a line: the minimum number of using superpower or output -1 if he can't leave.
Sample Input
4 1 4 4 1 3
Sample Output
0 1
Source
BestCoder Round #61 (div.2)
Recommend
hujie | We have carefully selected several similar problems for you: 5526 5525 5524 5522 5521
题目大意:
问题描述
XY在玩一个游戏:有N根柱子排成一排,编号为1到N,每个柱子上面有一块宝石,现在XY站在第S根柱子上,出口在第T跟柱子上,XY需要拿到所有宝石后从出口离开。每次XY可以走到相邻的柱子上,也可以使用超能力跳到第一根柱子或者第N根柱子上,如果离开了柱子之后再也不能到达这里。为了节省能量,XY想用最少次数超能力通关。
输入描述
输入有多组数据,不超过1000组. 每组数据输入一行包含3个整数,N,S和T.(1\leq N\leq10000,1\leq S,T\leq N )(1≤N≤10000,1≤S,T≤N)
输出描述
对于每组数据输出一行,表示使用超能力的最少次数,如果不可能离开,输出-1.
官方题解:
无解的情况只有起点和终点位置一样且N不为1。终点和起点都在边界上答案为0,如果起点在边界上或者起点终点相邻答案为1,其他答案为2.
个人想法:
分别讨论 s 与 t 的关系,
1)当 s == t 的时候,只有当 n == 1的时候输出0,其余输出-1;
2)当 s<t 的时候,再分别讨论 s == 1的时候 t的情况,相邻输出 1。
3)当 s>t 的时候,再分别讨论
s == n的时候 t的情况,相邻输出 1。
上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 15; const int mod = 1000000007; const double eps = 1e10-7; int main() { int n,s,t; while(~scanf("%d%d%d",&n,&s,&t)) { if(s == t) { if(n == 1) puts("0"); else printf("-1\n"); } else { if(s < t) { if(s==1 && t<n) puts("1"); else if(s==1 && t==n) puts("0"); else if(s != 1) { if(t == s+1) puts("1"); else puts("2"); } } else { if(s==n && t==1) puts("0"); else if(s==n && t>1) puts("1"); else if(s != n) { if(t == s-1) puts("1"); else puts("2"); } } } } return 0; }
相关文章推荐
- 英文标点
- Android开发者网址导航
- JAVA基础2-标识符、关键字、数据类型与运算符
- STL 中 lower_bound 与 upper_bound 与 二分查找
- uva 10570——Meeting with Aliens
- android studio中api 23的消息推送机制
- [leetcode 246] Strobogrammatic Number
- Android团队
- 【Linux 内核】文件系统(概念篇)
- 【疯狂的架构】牛公司组织结构图一览:华为,阿里,腾讯,百度,新浪……
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel
- HDU--3466(0-1背包+贪心/后效性)
- 【鸟哥的linux私房菜-学习笔记】Bash shell之管线命令
- ThinkPHP3.2.3中典型的ajax获取json数据方法
- codeforces #328 C. The Big Race
- 如何做到一个分数序列和
- python os目录和文件相关操作
- Coursera_Stanford_ML_ex7_K-means and PCA 作业记录
- 常见 门户布局
- JAVA基础1-JAVA入门和JDK的安装与配置