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报告”涉及到的是一个基于C++编程语言的迷宫问题解决算法。在这个项目中,我们关注的重点是如何在一个由0(通路)和1(障碍物)组成的m*n矩阵中,寻找从起点到终点的有效路径。 【描述】中...
《Maze.zip迷宫游戏开发》项目主要涉及的是利用Qt框架进行游戏开发,特别是迷宫游戏的实现。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式平台,支持C++语言,同时也提供了QML进行UI设计。 在...
本资源包" Maze-problem-data-structure.rar_迷宫问题 "包含了多个关于迷宫问题的源代码,旨在帮助深入理解和解决这类问题。下面我们将详细探讨迷宫问题的相关知识点。 1. **迷宫问题概述**:迷宫问题通常表现为在...
`maze.txt`可能包含迷宫的输入数据,如迷宫的布局、起点和终点的位置等。 在实际应用中,迷宫问题的解决方案可以用于游戏开发、路径规划、网络路由等领域。通过理解并实现这些算法,开发者可以提升其在问题解决和...
《Maze 迷宫游戏+完美注释》是一款基于C++编程语言开发的桌面游戏,其核心在于通过算法随机生成可玩的迷宫地图。在深入解析这款游戏之前,我们需要了解迷宫生成的基本概念和常见的算法。 迷宫生成算法有很多种,如...
界面美观,可以生成迷宫出去的路径,可以隐藏 项目效果: https://www.bilibili.com/video/BV1ww411o7oG/
1_maze迷宫.cpp
在这个过程中,`maze.is_valid(neighbor)`检查邻居是否为有效位置(即非障碍物),`maze.mark_as_visited(neighbor)`和`maze.mark_as_unvisited(neighbor)`用于跟踪已探索的路径,防止无限循环。 压缩包中的...
1. **数据结构设计**:这可能是自定义的`Maze`类,用于存储迷宫的状态,包括迷宫的大小、起始点和结束点,以及迷宫地图等。此外,可能还有`Node`类来表示路径上的节点,包括当前位置和前驱节点。 2. **搜索算法**:...
例如,可以创建一个`Maze`类来表示整个迷宫,包含迷宫的结构信息;一个`Player`类代表玩家,包含位置信息和移动行为;再如,可能还有一个`Cell`类来表示迷宫中的每一个格子,存储其状态(墙、路、起点、终点等)。...
在实际项目中,压缩包可能包含头文件、源文件以及主程序文件,如`Maze.h`、`Maze.cpp`和`main.cpp`。通过将这些文件组织在一起,我们可以方便地编译和运行整个项目。 总结起来,实现一个C++迷宫游戏涉及的主要知识...
《生成迷宫 Maze.abc》 迷宫生成是一种常见的编程挑战,它涉及到算法设计和数据结构的应用。在本文中,我们将深入探讨如何使用源码工具创建一个名为"Maze.abc"的迷宫生成器。首先,我们需要理解迷宫生成的基本原理...
RAR文件密码恢复软件(RAR.Password.Unlocker.v4.2.0.0.Cracked-MAZE)
maze.js 是一个专门用于生成迷宫的JavaScript库,它提供了高效且灵活的迷宫生成方法。本文将深入探讨maze.js库的核心概念、算法原理以及如何在实际项目中应用。 ### 1. 迷宫生成的基本概念 迷宫生成通常涉及到随机...
在这个项目中,“random-maze.zip”是一个包含FPGA迷宫游戏的压缩包,其重点是利用FPGA技术、PS2键盘接口以及VGA显示接口来创建一个互动游戏。 首先,我们要理解FPGA迷宫游戏的基本概念。FPGA迷宫游戏是通过编程在...
文件"maze"可能是描述迷宫的二维数组或邻接表的表示,而"www.pudn.com.txt"可能包含了题目描述、具体迷宫的输入数据或者是关于如何使用DFS解决迷宫问题的指南。通过解压并分析这些文件,可以更深入地理解DFS在解决...
总的来说,"maze_迷宫_强化学习_maze.py_mazepython"项目为我们提供了一个实际应用强化学习解决问题的例子,让我们能够直观地看到这种学习方法如何在动态环境中逐步优化决策策略。通过分析和理解这个项目,我们可以...
MATLAB基于guide的迷宫游戏-maze_game.zip maze_game.zip 包含深度优先,递归分割,prim算法三种类型 gaze.png gg.png
《3D迷宫:利用Three.js构建的交互式体验》 在数字世界中,3D图形技术为用户提供了丰富的视觉体验,特别是在游戏和互动应用领域。Three.js作为一款强大的JavaScript库,是WebGL编程的首选工具,使得在浏览器中创建...
用计算机生成迷宫是一个很有趣的任务。我们可以用 ? 道路网格(Road Grid)? ? 来表示迷宫的道路,那么 3 x 3 的 ? 道路网格? ( ? 图 -1 左? )可以对应一个 7 x 7 的 ? 渲染网格(Render Grid)? ——? 图 -1 右? 的方式...