Timus 1642. 1D Maze迷宫
2014-04-28 15:28
218 查看
1D people lived in a 1D country. Everything in the country was one-dimensional, and everything was simple and clear: just one axis and two directions — forward and backward. Even a 1D world has problems,
though; for instance, finding an exit from a maze. An idea of a 1D maze might seem weird to us, but not to 1D people. Escaping from such a maze is a hard and vital task for them. They solve this task in a following way.
A 1D person chooses a direction: backward (decreasing his coordinate) or forward (increasing it), and then moves in this direction. If he finds an exit, he escapes the maze immediately; if he encounters
an obstacle, he reverses his direction and continues walking.
In order to feel the hard life of 1D residents, try to implement a function that will compute a distance a 1D person will walk before finding an exit, based on the initial direction.
The first line contains space-separated integers n and x — the number of obstacles and the coordinate of an exit point (0 ≤ n ≤ 100). 1D person is located at
the origin. The second line contains n different integers — the coordinates of the obstacles. Each coordinate, including x, is non-zero and doesn't exceed 1000 in absolute value. No obstacle is located at the exit point. It is guaranteed
that 1D person will encounter either obstacle or exit point sooner or later regardless of the initial direction.
Output two space-separated integers — the distance a 1D person should walk before finding an exit if his initial direction is forward or backward, respectively. If he can't find the exit due to the
obstacles, output “Impossible”.
分好情况就能解决的问题。这里使用了数组,不过好像不需要使用数组,只需要记录靠零点最近的正负点就可以了。
需要细心的题目吧。
though; for instance, finding an exit from a maze. An idea of a 1D maze might seem weird to us, but not to 1D people. Escaping from such a maze is a hard and vital task for them. They solve this task in a following way.
A 1D person chooses a direction: backward (decreasing his coordinate) or forward (increasing it), and then moves in this direction. If he finds an exit, he escapes the maze immediately; if he encounters
an obstacle, he reverses his direction and continues walking.
In order to feel the hard life of 1D residents, try to implement a function that will compute a distance a 1D person will walk before finding an exit, based on the initial direction.
Input
The first line contains space-separated integers n and x — the number of obstacles and the coordinate of an exit point (0 ≤ n ≤ 100). 1D person is located atthe origin. The second line contains n different integers — the coordinates of the obstacles. Each coordinate, including x, is non-zero and doesn't exceed 1000 in absolute value. No obstacle is located at the exit point. It is guaranteed
that 1D person will encounter either obstacle or exit point sooner or later regardless of the initial direction.
Output
Output two space-separated integers — the distance a 1D person should walk before finding an exit if his initial direction is forward or backward, respectively. If he can't find the exit due to theobstacles, output “Impossible”.
Samples
input | output |
---|---|
3 -2 -10 -4 2 | 6 2 |
3 -2 10 -1 2 | Impossible |
需要细心的题目吧。
#include <vector> #include <iostream> using namespace std; void oneDMaze1642() { int n = 0, x = 0, a = 0; cin>>n>>x; vector<int> positive; vector<int> negative; for (int i = 0; i < n; i++) { cin>>a; if (a < 0) negative.push_back(a); else positive.push_back(a); } sort(positive.begin(), positive.end()); sort(negative.begin(), negative.end()); if ( positive.size() && positive[0] < x || negative.size() && negative.back() > x) cout<<"Impossible"; else if (x > 0) { cout<<x<<' '; if (negative.empty()) cout<<"Impossible"; else cout<<x - 2*negative.back(); } else { if (positive.empty()) cout<<"Impossible "; else cout<<2*positive[0] - x<<' '; cout<<-x; } }
相关文章推荐
- csharp:Convert Image to Base64 String and Base64 String to Image
- 嵌入式 获取linux下的网卡的MAC地址
- Qt5.2+VS2010安装
- ubuntu下搭建svn系列参考资料
- 解释器模式(待完成)
- java实现动态切换上网IP (ADSL拨号上网) java 断网重连 (原)读取cmd消息 乱码解决
- 数据库设计范式
- iOS设计模式:静态工厂相关
- MAMP PRO 下安装 memcache
- C#:NumericUpDown控件(可调节类控件)
- 人格巫妖王
- 学习怎可一知半解
- 黑马程序员_学习笔记:7) 多线程、同步
- ios更改Xcode配置
- 使用json方式上传图片时,发现上传成功后,变成下载了json... 或者无法解析到返回的结果集!!!
- ProgressTimer
- android UI开源项目
- UBUNTU系统关闭开机输入用户密码的方法
- 使用NSHashTable存储引用对象
- 游戏素材网站大全