Moving By Points (topcoder SRM 738 DIV 2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuxufanzhong/article/details/82952980

Problem Statement

You are given N points. The points are numbered from 0 to N-1. Point i has coordinates (X[i], Y[i]). Note that the given points are not necessarily distinct.

You have to move from point 0 to point N-1. You have to move in steps. In each step you can move by 1 unit of distance in one of the four cardinal directions. That is, if you are at coordinates (x,y), you can move to (x-1,y), (x+1,y), (x,y+1), or (x,y-1). Additionally, you can only step on points, you cannot enter coordinates that don’t contain a point.

For the given N points it may be impossible to get from point 0 to point N-1 according to the above rules. Compute and return the smallest number of additional points that have to be added to the collection in order to be able to do that.

Definition

Class: MovingByPoints
Method: countMinimumPoints
Parameters: int, vector <int>, vector <int>
Returns: int
Method signature: int countMinimumPoints(int N, vector <int> X, vector <int> Y)
(be sure your method is public)
Limits

Time limit (s): 2.000
Memory limit (MB): 256
Constraints

  • N will be between 1 and 500, inclusive.
  • X and Y will each have exactly N elements.
  • Each element of X and Y will be between 1 and 10^6, inclusive.

Examples

(0)

3
{0,1,2}
{0,1,2}
Returns: 2

You want to be able to walk from (0,0) to (2,2). In order to be able to do that, you need to add two more points. One of multiple optimal solutions is to add the points (1,0) and (1,2).
(1)

4
{1,1,1,3}
{1,2,3,3}
Returns: 1

(2)

10
{1,1,1,2,3,3,4,5,5,5}
{1,2,3,3,3,2,2,2,3,4}
Returns: 0

(3)

7
{2,3,4,5,6,6,5}
{2,1,1,1,2,3,4}
Returns: 3

(4)

2
{2,2}
{2,2}
Returns: 0

Here you want to travel from (2,2) to (2,2). That is trivially possible, without having to add more points.

Note that the point (2,2) is given twice. This has exactly the same effect as if it were given only once.

(5)

20
{260522,436426,648772,933447,703497,407775,963982,968417,631932,895728,723857,286918,539679,63340,361868,287940,224593,836991,823355,11431}
{914575,979445,690081,190629,47202,894325,804784,302156,735902,78537,330739,329211,238506,686568,660016,296263,601449,890310,177068,8580}
Returns: 1155084

题目大意

在一个二维坐标系中,有N个整数点,编号是0…N-1,坐标是(X[i],Y[i])。在初始状态下,只有这n个点可以走,其他点均有障碍物不能走。每次走可以选择往择上下左右相邻的任何一个没有障碍物的点走。问,最少去除多少个障碍物,使得可以从0号点走到N-1号点。
1<=N<=500, 1<=X[i],Y[i]<=10^6

解答

解答和AC代码将在一周后给出。

AC代码

class MovingByPoints
{
public:
    int countMinimumPoints(int N, vector<int> X, vector<int> Y)
    {

    }
};

猜你喜欢

转载自blog.csdn.net/wuxufanzhong/article/details/82952980