描述
A robot is located in a pair of integer coordinates (x, y). It must be moved to a location with another set of coordinates. Though the bot can move any number of times, it can only make the following two types of moves:
Given the start coordinates and the end coordinates of the target, if the robot can reach the end coordinates according to the given motion rules, you should return true, otherwise you should return false.
x 和 y 的范围是: [1, 1 000 000 000]
移动的次数可以是0次。
示例 1:
输入:
start = [1, 1]
target = [5, 2]
输出:
True
解释:
(1, 1) -> (1, 2) -> (3, 2) -> (5, 2), 你应该返回True
示例 2:
输入:
start = [1, 2]
target = [3, 4]
输出:
False
解释:
[1, 2] 根据移动规则不能到达 [3, 4] 所以你应该返回False.
示例 3:
输入:
start = [2, 1]
target = [4, 1]
输出:
True
解释:
(2, 1) -> (3, 1) -> (4, 1), 你应该返回True.
class Solution {
public:
/**
* @param start: a point [x, y]
* @param target: a point [x, y]
* @return: return True and False
*/
bool ReachingPoints(vector<int> &start, vector<int> &target) {
if(start == target) return true;
while(target[0] >= start[0] && target[1] >= start[1])
{
if(start == target)
return true;
if(target[0] == start[0])//有一个坐标x已经相同了
{
if((target[1]-start[0])%start[0] == 0)
return true;//另一个坐标 y 一直减 x 即可,是倍数关系 true
else
target[1] -= start[0];
}
else if(target[1] == start[1])
{
if((target[0]-start[1])%start[1] == 0)
return true;
else
target[0] -= start[1];
}
else if(target[0] > target[1])
target[0] -= target[1];
else
target[1] -= target[0];
}
return false;
}
};