Timus 1642. 1D Maze迷宫

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 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

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;
	}
}

Timus 1642. 1D Maze迷宫,码迷,mamicode.com

时间: 2024-10-13 06:03:52

Timus 1642. 1D Maze迷宫的相关文章

Maze迷宫问题

迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用Pos来记录: struct Pos    //位置坐标 {    int  _row;    int _col; }; 定义行列范围 #define M 10   //行 #define N 10   //列 初始化迷宫数组:将通过读文件的方式获取的字符转成整型数据,保存在M行N列的数组中. void InitMaze(int* maze) { struct WavHeadhWAV; FILE* 

Maze迷宫问题(求最优解)

迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; 定义行列范围: #define M 10 //行 #define N 10 //列 初始化迷宫数组:将通过读文件的方式获取的字符转成整型数据,保存在M行N列的数组中. void InitMaze(int* maze) { struct WavHeadhWAV; FILE* fout = fopen(

学霸的迷宫(BFS+记录路径)

1 //求从(sx.sy)到(gx.gy)的最短距离; 2 3 #include<iostream> 4 #include<cstdio> 5 #include<cstdio> 6 #include<queue> 7 #include<cstring> 8 #define INF 99999999 9 10 using namespace std; 11 12 typedef pair<int, int > P; //数对,记录位置

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

栈与回溯:迷宫问题

迷宫问题是栈的典型应用,栈通常也与回溯算法连用. 回溯算法的基本描述是: (1)  选择一个起始点: (2)  如果已达目的地, 则跳转到 (4): 如果没有到达目的地, 则跳转到 (3) ; (3)  求出当前的可选项: a.  若有多个可选项,则通过某种策略选择一个选项,行进到下一个位置,然后跳转到 (2); b.  若行进到某一个位置发现没有选项时,就回退到上一个位置,然后回退到 (2) ; (4) 退出算法. 在回溯算法的实现中,通常要使用栈来保存行进中的位置及选项.本文给出自己写的迷宫

7.7 迷宫求解

7-8 Maze1.c 1 #include <stdio.h> 2 #define MAXLEN 30 // 迷宫包括外墙最大行列数目 3 #define INIT_SIZE 100 // 存储空间初始分配量 4 typedef struct 5 { 6 int row; //迷宫的行数 7 int column; //迷宫的列数 8 char grid[MAXLEN][MAXLEN]; //1表示障碍,0表示空,2表示可通,3表示已走过但不通 9 }MazeType; // 迷宫类型 10

数据结构之迷宫问题求解(二)迷宫的最短路径

上篇文章我们讨论了,迷宫问题的普通求解问题,这篇文章我们继续深入,求迷宫的最短路径. 要想求迷宫的最短路径,一个很简单的方法就是再设置一个Min栈,用来放最短路径,每找到一个出口,就将path栈与Min栈进行比较,如果path栈更小,则赋值给Min. 而在上篇文章中,我们将走过的路径做了标记,每走一个坐标,就把那个坐标置为3,直至找到出口. 因此如果用这种标记方式,显然是会出现问题的. 所以我们需要换种标记方式! 最终....我决定,使出口的值为2,每走一步使当前位置标记变为是上一位置标记再加1

uva 784 Maze Exploration(DFS遍历图)

uva 784 Maze Exploration A maze(迷宫) of rectangular(矩形的) rooms is represented on a two dimensional(空间的) grid as illustrated(阐明) in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same charact

迷宫问题求解(一)利用栈与递归求解出口

本文适合于对迷宫问题已有初步研究,或阅读代码能力较强的人. 因此,如果你对迷宫问题一无所知,请参考其他更详细的资料. 迷宫问题,是一个对栈(Stack)典型应用的例子之一. 假如,有如下10X10的迷宫(0代表通路,1代表障碍),我们需要用写程序来找出迷宫的出口. 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 0