Moving By Points (topcoder SRM 738 DIV 2)
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)
{
}
};