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.
Input
The first line contains space-separated integersnandx— the number of obstacles and the coordinate of an exit point<nobr>(0 ≤<em>n</em>≤ 100)</nobr>. 1D person is located at
the origin. The second line containsndifferent integers— the coordinates of the obstacles. Each coordinate, includingx, 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 the
obstacles, 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;
}
}
分享到:
相关推荐
maze迷宫cpp报告 问题描述:以一个m*n的长方阵表示迷宫,0,1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论,
界面美观,可以生成迷宫出去的路径,可以隐藏 项目效果: https://www.bilibili.com/video/BV1ww411o7oG/
1_maze迷宫.cpp
Maze.zip迷宫游戏开发
迷宫问题是比较经典的算法设计题,改C程序较好的实现了迷宫问题。
算法与数据结构中 几类迷宫问题的源码 深入学习迷宫问题
MATLAB基于guide的迷宫游戏-maze_game.zip maze_game.zip 包含深度优先,递归分割,prim算法三种类型 gaze.png gg.png
用计算机生成迷宫是一个很有趣的任务。我们可以用 ? 道路网格(Road Grid)? ? 来表示迷宫的道路,那么 3 x 3 的 ? 道路网格? ( ? 图 -1 左? )可以对应一个 7 x 7 的 ? 渲染网格(Render Grid)? ——? 图 -1 右? 的方式...
小老鼠走迷宫,迷宫代码,使用C++完成,质量良好
代码架构迷宫生成方法DFSKruskal最小树递归代码架构home.html 用浏览器打开,主页maze_dfs_back.js 用dfs+回溯求解maze_kruskal_mst.js 用最小生成树生成maze_recur.js 递归生成迷宫生成方法DFS迷宫的一个小方格...
dfs 深度优先搜索!这是走迷宫的基本算法。用广义表建立迷宫,用邻接表建立图,用dfs搜索,许多大学的数据结构作业
字母A~Z走迷宫maze,如图成功: A B C . . . . . . . . . D . . . . . . . . . E . . . . . . . . . F . . . . . . . . . G H . . . . . . . . . I . . . . . . . . . J . . . X W V . . . K L M Z Y T U ....
maze.js 迷宫生成在javascript中。
题目为慕课网C++课后题迷宫游戏Maze。题目链接:https://www.imooc.com/video/8207。 代码为本人原创,配合文章《慕课网C++课后题迷宫游戏Maze》。 链接:...
一个用java写的迷宫小程序,能自动生成迷宫,并使用深度优先和广度优先算法解迷宫
Modbus.Poll.v5.0.1.Build.450.Cracked-MAZE ; Modbus主机仿真
这是一个迷宫游戏,每次重绘迷宫都是随机生成的~~~~~~~~
Modbus.Slave.v4.4.5.Build.448.Cracked-MAZE;Modbus从机仿真
采用verilg语言并结合VGA显示、PS2接口技术、键盘输入等实现基于FPGA开发板的可选择性迷宫游戏。可以利用电脑键盘和显示器来玩这个游戏
基于python实现的强化学习,用tensorflow实现迷宫程序