美文网首页Leetcode
Leetcode 874. Walking Robot Simu

Leetcode 874. Walking Robot Simu

作者: SnailTyan | 来源:发表于2019-01-31 09:43 被阅读1次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Walking Robot Simulation

    2. Solution

    class Solution {
    public:
        int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
            int x = 0;
            int y = 0;
            int maximum = 0;
            char directions[] = {'N', 'E', 'S', 'W'};
            int index = 0;
            set<pair<int, int>> obstacleSet;
            for (vector<int> obstacle: obstacles) {
                obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));
            }
            for(int i = 0; i < commands.size(); i++) {
                if(commands[i] == -1) {
                    index = (index + 1) % 4;
                }
                else if(commands[i] == -2) {
                    index = (index - 1 + 4) % 4;
                }
                else {
                    for(int j = 0; j < commands[i]; j++) {
                        moveForward(obstacleSet, x, y, directions[index]);
                        maximum = max(maximum, x*x + y*y);
                    }
                }
            }
            return maximum;
        }
    
    
    private:
        void moveForward(set<pair<int, int>>& obstacleSet, int& x, int& y, char direction) {
            switch(direction) {
                case 'N':
                    if (obstacleSet.find(make_pair(x, y + 1)) == obstacleSet.end()) {
                        y += 1;
                    }
                    break;
                case 'E':
                    if (obstacleSet.find(make_pair(x + 1, y)) == obstacleSet.end()) {
                        x += 1;
                    }
                    break;
                case 'S':
                    if (obstacleSet.find(make_pair(x, y - 1)) == obstacleSet.end()) {
                        y -= 1;
                    }
                    break;
                case 'W':
                    if (obstacleSet.find(make_pair(x - 1, y)) == obstacleSet.end()) {
                        x -= 1;
                    }
                    break;
            }
        }
    };
    

    Reference

    1. https://leetcode.com/problems/walking-robot-simulation/description/

    相关文章

      网友评论

        本文标题:Leetcode 874. Walking Robot Simu

        本文链接:https://www.haomeiwen.com/subject/whcksqtx.html