874.Walking Robot Simulation(list不可被哈希)

A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:

  • -2: turn left 90 degrees
  • -1: turn right 90 degrees
  • 1 <= x <= 9: move forward x units

Some of the grid squares are obstacles.

The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])

If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

Return the square of the maximum Euclidean distance that the robot will be from the origin.

Example 1:

Input: commands = [4,-1,3], obstacles = []

Output: 25

Explanation: robot will go to (3, 4)

Example 2:

Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]

Output: 65

Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)

Note:

  • 0 <= commands.length <= 10000
  • 0 <= obstacles.length <= 10000
  • -30000 <= obstacle[i][0] <= 30000
  • -30000 <= obstacle[i][1] <= 30000
  • The answer is guaranteed to be less than 2 ^ 31.

Solution1:(TLE)

class Solution:
    def robotSim(self, commands, obstacles):
        """
        :type commands: List[int]
        :type obstacles: List[List[int]]
        :rtype: int
        """
        pos = [0,0]
        direction = 0
        def turn(pre,new):
            if new == -1:
                now = (pre + 1)%4
            else:
                now = (pre - 1)%4
            return now
        def move(direction,distance):
            if direction==0:
                while distance>0:
                    distance -= 1
                    pos[1] += 1
                    if pos in obstacles:
                        pos[1] -= 1
                        break
            if direction==1:
                while distance>0:
                    distance -= 1
                    pos[0] += 1
                    if pos in obstacles:
                        pos[0] -= 1
                        break
            if direction==2:
                while distance>0:
                    distance -= 1
                    pos[1] -= 1
                    if pos in obstacles:
                        pos[1] += 1
                        break
            if direction==3:
                while distance>0:
                    distance -= 1
                    pos[0] -= 1
                    if pos in obstacles:
                        pos[0] += 1
                        break
            return
        res = 0
        for i in commands:
            # print(i)
            if i>0:
                move(direction,i)
                # print(‘pos:‘,pos,"direction:",direction)
            else:
                direction = turn(direction,i)
                # print(‘pos:‘,pos,"direction:",direction)
            res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
        return res

有一个结果过不了,百思不得其解,画了图对照走了一遍也没有错。最后发现返回的结果是中间任意时刻离原点的最大值,而不是最终的结果,太坑了。

Solution2:

class Solution:
    def robotSim(self, commands, obstacles):
        """
        :type commands: List[int]
        :type obstacles: List[List[int]]
        :rtype: int
        """
        s = set()
        for i in obstacles:
            s.add(tuple(i))
        pos = [0,0]
        direction = 0
        def turn(pre,new):
            if new == -1:
                now = (pre + 1)%4
            else:
                now = (pre - 1)%4
            return now
        def move(direction,distance):
            if direction==0:
                while distance>0:
                    distance -= 1
                    pos[1] += 1
                    if tuple(pos) in s:
                        pos[1] -= 1
                        break
            if direction==1:
                while distance>0:
                    distance -= 1
                    pos[0] += 1
                    if tuple(pos) in s:
                        pos[0] -= 1
                        break
            if direction==2:
                while distance>0:
                    distance -= 1
                    pos[1] -= 1
                    if tuple(pos) in s:
                        pos[1] += 1
                        break
            if direction==3:
                while distance>0:
                    distance -= 1
                    pos[0] -= 1
                    if tuple(pos) in s:
                        pos[0] += 1
                        break
            return
        res = 0
        for i in commands:
            # print(i)
            if i>0:
                move(direction,i)
                # print(‘pos:‘,pos,"direction:",direction)
            else:
                direction = turn(direction,i)
                # print(‘pos:‘,pos,"direction:",direction)
            res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
        return res

改用set存储obstacle而不是list,不再超时了。set查找元素是哈希的方法,需要O(1),而list则是O(n)。

注意list是不可以被哈希的,故改用tuple来存储到set中。

原文地址:https://www.cnblogs.com/bernieloveslife/p/9751086.html

时间: 2024-11-06 09:30:14

874.Walking Robot Simulation(list不可被哈希)的相关文章

【Leetcode_easy】874. Walking Robot Simulation

problem 874. Walking Robot Simulation 参考 1. Leetcode_easy_874. Walking Robot Simulation; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11252142.html

874. Walking Robot Simulation

A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forward x units Some of the grid squares are obs

[LeetCode] Walking Robot Simulation 走路机器人仿真

A robot on an infinite grid starts at point (0, 0) and faces north.? The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forward?x?units Some of the grid squares are obs

leetcode 874 Robot Simulation

leetcode 874 Robot Simulation 限制:32MB, 1s 描述 A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forw

Codeforces 1154D - Walking Robot - [贪心]

题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可充电电池能够充一格电,就用普通电池跑(让可充电池充电),否则就用可充电电池走. AC代码: #include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; int n,a,b; bool s[maxn]; int main()

B - Snow Walking Robot

题意:给出一段表方向的字符串,u.d.l.r分别表示向上.向下.向左.向右,让你重新排列,使其走出去再回到原点,除了原点能走两次以外其他点都只能走一次,输              出走的次数和走法. 思路:最简单的走法:就是绕一圈,先全是上,再全是右,全是下,全是左,即上的次数==下的次数,左的次数==右的次数,求两组对应方向的较小值,两值之和的两倍            即是走的次数. 有一个方向出现次数是0,则对应的反方向则也为0,另外两个方向就只能各一个,例如:上出现次数是0,则只有一种

Codeforces 1296C - Yet Another Walking Robot

题目大意: 给定一个机器人的行走方式 你需要取走一段区间 但要保证取走这段区间后机器人最终到达的终点位置是不变的 问这段区间最短时是哪一段 解题思路: 易得,如果重复走到了某些已经走过的点,那么肯定就有一段区间可以被删除 但是行走次数最大有2e5,即用数组记录坐标状态的话起码要开4e5*4e5的空间,显然不可能 所以可以用map储存上一次走到某个坐标是第几步 那么每次只要判断当前的坐标是否已经被走过即可,走过的话就尝试更新答案 因为map中未调用过的int值为0 所以让原点的步数设置为1防止混淆

Codeforces Round #617 (Div. 3) C. Yet Another Walking Robot

http://codeforces.com/contest/1296/problem/C 题意:给一段字符串表示移动,然后求删除最短的一段,并且不影响结果 题解: 意思是:建立pair点和map,当遍历到第i个点有一个pair值,把这个加到map里面,如果向后接着遍历时出现与i点相同的pair值时,那么这一段表示可以删除的一段 #include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen(&qu

【leetcode】Weekly Contest 94

题目不难,被第二题卡了半个多小时QAQ,另一个就是以后能用Hashmap和Hashset的绝不遍历. 1. Leaf-Similar Trees dfs.层次遍历把叶子节点遍历然后对比即可,只要是先遍历左节点后遍历右节点就行. 1 class Solution { 2 public boolean leafSimilar(TreeNode root1, TreeNode root2) { 3 ArrayList<Integer> res1 = new ArrayList<>();