Skip to content

Latest commit

 

History

History
230 lines (192 loc) · 6.45 KB

File metadata and controls

230 lines (192 loc) · 6.45 KB

English Version

题目描述

机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands

  • -2 :向左转 90
  • -1 :向右转 90
  • 1 <= x <= 9 :向前移动 x 个单位长度

在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点  obstacles[i] = (xi, yi)

机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。

返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5 ,则返回 25

 

注意:

  • 北表示 +Y 方向。
  • 东表示 +X 方向。
  • 南表示 -Y 方向。
  • 西表示 -X 方向。

 

示例 1:

输入:commands = [4,-1,3], obstacles = []
输出:25
解释:
机器人开始位于 (0, 0):
1. 向北移动 4 个单位,到达 (0, 4)
2. 右转
3. 向东移动 3 个单位,到达 (3, 4)
距离原点最远的是 (3, 4) ,距离为 32 + 42 = 25

示例 2:

输入:commands = [4,-1,4,-2,4], obstacles = [[2,4]]
输出:65
解释:机器人开始位于 (0, 0):
1. 向北移动 4 个单位,到达 (0, 4)
2. 右转
3. 向东移动 1 个单位,然后被位于 (2, 4) 的障碍物阻挡,机器人停在 (1, 4)
4. 左转
5. 向北走 4 个单位,到达 (1, 8)
距离原点最远的是 (1, 8) ,距离为 12 + 82 = 65

 

提示:

  • 1 <= commands.length <= 104
  • commands[i] is one of the values in the list [-2,-1,1,2,3,4,5,6,7,8,9].
  • 0 <= obstacles.length <= 104
  • -3 * 104 <= xi, yi <= 3 * 104
  • 答案保证小于 231

解法

方法一:模拟

Python3

class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        s = {(x, y) for x, y in obstacles}
        ans, p = 0, 1
        x = y = 0
        for v in commands:
            if v == -2:
                p = (p + 3) % 4
            elif v == -1:
                p = (p + 1) % 4
            else:
                for _ in range(v):
                    nx, ny = x + dirs[p][0], y + dirs[p][1]
                    if (nx, ny) in s:
                        break
                    x, y = nx, ny
                    ans = max(ans, x * x + y * y)
        return ans

Java

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        Set<String> s = new HashSet<>();
        for (int[] v : obstacles) {
            s.add(v[0] + "." + v[1]);
        }
        int ans = 0, p = 1;
        int x = 0, y = 0;
        for (int v : commands) {
            if (v == -2) {
                p = (p + 3) % 4;
            } else if (v == -1) {
                p = (p + 1) % 4;
            } else {
                while (v-- > 0) {
                    int nx = x + dirs[p][0], ny = y + dirs[p][1];
                    if (s.contains(nx + "." + ny)) {
                        break;
                    }
                    x = nx;
                    y = ny;
                    ans = Math.max(ans, x * x + y * y);
                }
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
        vector<vector<int>> dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        unordered_set<string> s;
        for (auto v : obstacles) s.insert(to_string(v[0]) + "." + to_string(v[1]));
        int ans = 0, p = 1;
        int x = 0, y = 0;
        for (int v : commands) {
            if (v == -2)
                p = (p + 3) % 4;
            else if (v == -1)
                p = (p + 1) % 4;
            else {
                while (v--) {
                    int nx = x + dirs[p][0], ny = y + dirs[p][1];
                    if (s.count(to_string(nx) + "." + to_string(ny))) break;
                    x = nx;
                    y = ny;
                    ans = max(ans, x * x + y * y);
                }
            }
        }
        return ans;
    }
};

Go

func robotSim(commands []int, obstacles [][]int) int {
	dirs := [][]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
	s := map[string]bool{}
	for _, v := range obstacles {
		t := strconv.Itoa(v[0]) + "." + strconv.Itoa(v[1])
		s[t] = true
	}
	ans, p := 0, 1
	x, y := 0, 0
	for _, v := range commands {
		if v == -2 {
			p = (p + 3) % 4
		} else if v == -1 {
			p = (p + 1) % 4
		} else {
			for i := 0; i < v; i++ {
				nx, ny := x+dirs[p][0], y+dirs[p][1]
				t := strconv.Itoa(nx) + "." + strconv.Itoa(ny)
				if s[t] {
					break
				}
				x, y = nx, ny
				ans = max(ans, x*x+y*y)
			}
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

...