zoukankan      html  css  js  c++  java
  • 30 Day Challenge Day 17 | Leetcode 489. Robot Room Cleaner

    题解

    Hard

    BFS

    class Solution {
    public:
        void cleanRoom(Robot& robot) {
            dfs(robot, 0, 0, 0);
        }
    
        void goback(Robot &r) {
            r.turnLeft();
            r.turnLeft();
            r.move();
            r.turnRight();
            r.turnRight();
        }
    
        /*
         * d: new direction in the map
         */
        void dfs(Robot &robot, int i, int j, int d) {
            string pos = to_string(i) + '-' + to_string(j);
    
            if (visited.count(pos)) {
                return;
            }
    
            robot.clean();
    
            visited.insert(pos);
    
            // 0: 'up', 1: 'right', 2: 'down', 3: 'left'
            for (int k = 0; k < 4; k++, d++) {
                d = d % 4;
                if (robot.move()) {
                    dfs(robot, i + dir[d][0], j + dir[d][1], d);
                    goback(robot);
                }
                robot.turnRight(); // clockwise motion
            }
        }        
    
    private:
        unordered_set<string> visited;
        int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    };
    
  • 相关阅读:
    nginx一键安装脚本
    nginx动静分离之后,设置默认主页
    日志备份
    cc高防主机部署
    原型和原型链
    Git&Github分支
    Git&Github基础
    传输层协议TCP&UDP
    本地库与远程库交互
    SVG
  • 原文地址:https://www.cnblogs.com/casperwin/p/13760730.html
Copyright © 2011-2022 走看看