天池 在线编程 到达终点

文章目录

1. 题目

描述

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:

  • From location (x, y) to location (x + y, y)
  • From location (x, y) to location (x, x + y)

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.

https://tianchi.aliyun.com/oj/338602522508251883/378934605959598749

2. 解题

  • 从终点往回推,大的坐标减去小的坐标
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;
    }
};

我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页