码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] 874. 行走机器人模拟(周赛)

时间:2018-07-22 12:00:13      阅读:615      评论:0      收藏:0      [点我收藏+]

标签:模拟   tac   leetcode   bre   str   ash   bst   com   put   

874. 行走机器人模拟

模拟

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int max = 0;
        int[][] dx = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int k = 0;
        Map<String, Boolean> map = new HashMap<>();
        for (int i = 0; i < obstacles.length; i++) {
            map.put(obstacles[i][0] + "," + obstacles[i][1], true);
        }
        int p = 0, q = 0;
        for (int command : commands) {
            if (command == -1) {
                k = (k + 1) % 4;
            } else if (command == -2) {
                k = (k + 4 - 1) % 4;
            } else {
                int cur[] = dx[k];
                for (int i = 0; i < command; i++) {
                    if (map.containsKey((p + cur[0]) + "," + (q + cur[1]))) {
                        break;
                    }
                    p += cur[0];
                    q += cur[1];
                }
                max = Math.max(max, p * p + q * q);
            }
        }
        return max;
    }
}

[leetcode] 874. 行走机器人模拟(周赛)

标签:模拟   tac   leetcode   bre   str   ash   bst   com   put   

原文地址:https://www.cnblogs.com/acbingo/p/9349541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!