Leetcode:874. 模拟行走机器人

题目:

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

-2:向左转 90 度
-1:向右转 90 度
1 <= x <= 9:向前移动 x 个单位长度
在网格上有一些格子被视为障碍物。

第 i 个障碍物位于网格点  (obstacles[i][0], obstacles[i][1])

如果机器人试图走到障碍物上方,那么它将停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。

返回从原点到机器人的最大欧式距离的平方。

示例 1:

输入: commands = [4,-1,3], obstacles = []
输出: 25
解释: 机器人将会到达 (3, 4)
示例 2:

输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
输出: 65
解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处

AC代码;

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        Map<Integer,Map<Integer,Integer>> obstacle = new HashMap<>();
        for(int i = 0;i<obstacles.length;++i){
            if(obstacle.containsKey(obstacles[i][0])){
                obstacle.get(obstacles[i][0]).put(obstacles[i][1],1);
            }
            else{
                Map<Integer,Integer> temp = new HashMap<>();
                temp.put(obstacles[i][1],1);
                obstacle.put(obstacles[i][0],temp);
            }
        }
        int []ans = {0,0};
        int max = 0;
        //上右下左
        int direct = 0;
        for(int i=0;i!=commands.length;++i){
            if(-1 == commands[i]){
                direct = (direct + 1) % 4;
            }
            else if(-2 == commands[i]){
                direct = (direct + 3) % 4;
            }
            else{
                //上
                if(direct == 0){
                    for(int j=0;j!=commands[i];++j){
                        if(obstacle.containsKey(ans[0]) && obstacle.get(ans[0]).containsKey(ans[1] + 1)){
                            break;
                        }
                        else{
                            ++ans[1];
                        }
                    }
                }
                //右
                else if(direct == 1){
                    for(int j=0;j!=commands[i];++j){
                        if(obstacle.containsKey(ans[0] + 1) && obstacle.get(ans[0] + 1).containsKey(ans[1])){
                            break;
                        }
                        else{
                            ++ans[0];
                        }
                    }
                }
                //下
                else if(direct == 2){
                    for(int j=0;j!=commands[i];++j){
                        if(obstacle.containsKey(ans[0]) && obstacle.get(ans[0]).containsKey(ans[1] - 1)){
                            break;
                        }
                        else{
                            --ans[1];
                        }
                    }
                }
                //左
                else{
                    for(int j=0;j!=commands[i];++j){
                        if(obstacle.containsKey(ans[0] - 1) && obstacle.get(ans[0] - 1).containsKey(ans[1])){
                            break;
                        }
                        else{
                            --ans[0];
                        }
                    }
                }
                int tempDistance = ans[0] * ans[0] + ans[1] * ans[1];
                max = max > tempDistance ? max : tempDistance;
            }
        }
        return max;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值