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 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:

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

Approach #1: C++.

class Solution {
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
        vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int x = 0, y = 0, di = 0;
        int ans = 0;

        set<pair<int, int>> obstacleSet;
        for (auto obstacle : obstacles)
            obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));

        for (int command : commands) {
            if (command == -2) {
                di = (di + 3) % 4;
            } else if (command == -1) {
                di = (di + 1) % 4;
            } else {
                for (int i = 0; i < command; ++i) {
                    int nx = x + dirs[di].first;
                    int ny = y + dirs[di].second;
                    if (obstacleSet.find(make_pair(nx, ny)) == obstacleSet.end()) {
                        x = nx;
                        y = ny;
                        ans = max(ans, x*x + y*y);
                    }
                }
            }
        }
        return ans;
    }
};

  

Analysis:

If we know the relation of the directions and turn, it will become easier.

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10311975.html

时间: 2024-11-01 20:15:10

874. Walking Robot Simulation的相关文章

【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(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 obst

[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<>();