zoukankan      html  css  js  c++  java
  • 489. Robot Room Cleaner

    package LeetCode_489
    
    /**
     * 489. Robot Room Cleaner
     * (Prime)
     * Given a robot cleaner in a room modeled as a grid.
    Each cell in the grid can be empty or blocked.
    The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.
    When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
    Design an algorithm to clean the entire room using only the 4 given APIs shown below.
    interface Robot {
    // returns true if next cell is open and robot moves into the cell.
    // returns false if next cell is obstacle and robot stays on the current cell.
    boolean move();
    // Robot will stay on the same cell after calling turnLeft/turnRight.
    // Each turn will be 90 degrees.
    void turnLeft();
    void turnRight();
    // Clean the current cell.
    void clean();
    }
    Example:
    Input:
    room = [
    [1,1,1,1,1,0,1,1],
    [1,1,1,1,1,0,1,1],
    [1,0,1,1,1,1,1,1],
    [0,0,0,1,0,0,0,0],
    [1,1,1,1,1,1,1,1]
    ],
    row = 1,
    col = 3
    
    Explanation:
    All grids in the room are marked by either 0 or 1.
    0 means the cell is blocked, while 1 means the cell is accessible.
    The robot initially starts at the position of row=1, col=3.
    From the top left corner, its position is one row below and three columns right.
    
    Notes:
    1. The input is only given to initialize the room and the robot's position internally.You must solve this problem "blindfolded".
    In other words, you must control the robot using only the mentioned 4 APIs,
    without knowing the room layout and the initial robot's position.
    2. The robot's initial position will always be in an accessible cell.
    3. The initial direction of the robot will be facing up.
    4. All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
    5. Assume all four edges of the grid are all surrounded by wall.
     * */
    
    interface Robot {
        // returns true if next cell is open and robot moves into the cell.
        // returns false if next cell is obstacle and robot stays on the current cell.
        fun move(): Boolean
    
        // Robot will stay on the same cell after calling turnLeft/turnRight.
        // Each turn will be 90 degrees.
        fun turnLeft()
    
        fun turnRight()
        // Clean the current cell.
        fun clean()
    }
    
    /*
    * solution: DFS + Backtracking, Time complexity:O(mn), Space complexity:O(mn)
    * */
    class Solution {
    
        val directions = intArrayOf(0, 1, 0, -1, 0)
    
        fun cleanRoom(robot: Robot) {
            val visited = HashSet<String>()
            //start from 0,0 and facing up
            dfs(robot, 0, 0, directions[0], visited)
        }
    
        private fun dfs(robot: Robot, x: Int, y: Int, direction: Int, visited: HashSet<String>) {
            //execute clean while reach some place
            robot.clean()
            val key = "$x-$y"
            visited.add(key)
            //try 4 directions in clockwise: 0->up, 1->right, 2->down, 3->left
            for (i in 0 until 4) {
                //keep cur in 0-4
                val cur = (i + direction) % 4
                val newX = x + directions[cur]
                val newY = y + directions[cur + 1]
                //check new x,y if not visited and can move to
                if (!visited.contains("$newX-$newY") && robot.move()) {
                    dfs(robot, newX, newY, cur, visited)
                    //do below like backtracking
                    //turn 180 degree to back to former cell
                    robot.turnRight()
                    robot.turnRight()
                    robot.move()
                    //turn 180 degree to back to init facing direction
                    robot.turnRight()
                    robot.turnRight()
                }
                //when after doing up to the end
                //update robot facing direction to right, because init is facing up
                robot.turnRight()
            }
        }
    }
  • 相关阅读:
    文件下载并删除
    程序输出
    什么是并发
    跨站脚本攻击为什么要过滤危险字符串
    正则表达式——极速入门
    输出的为X= 1 ,Y = 0;
    Eclipse+MyEclipse安装及环境配置
    J2EE有三个开发环境级。
    TCP和UDP的区别
    asp.net2.0导出pdf文件完美解决方案(转载)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13795927.html
Copyright © 2011-2022 走看看