일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- SSAFY 테스트
- 자고 싶다
- 아자아자 화이팅
- SeongSeobDang
- Hamming weight
- 우유가옆으로넘어지면아야
- HAVE A GOOD DAY
- Java 환경 설정
- amazon
- 수학
- SSAFY IM/A
- BFS
- 모르고리즘
- Have a good day :)
- 코로나 싫어요
- SSAFY 화이팅
- 텐션 업 10기 화이팅
- I am Korean
- 텐션 업 10기!
- SSAFY 10기 화이팅
- 우유아야
- 네트워크
- 우유가 옆으로 넘어지면 아야
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- DP
- DFS
- 자료구조
- Have a nice day.
- have a nice day
- Today
- Total
Hope Everyone Is Happy
[Hard] 489. Robot Room Cleaner (Java) 본문
[Hard] 489. Robot Room Cleaner (Java)
J 크 2023. 12. 11. 11:22https://leetcode.com/problems/robot-room-cleaner/description/
Robot Room Cleaner - LeetCode
Can you solve this real interview question? Robot Room Cleaner - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제가 신기해
※ Question Summary
▶ You are controlling a robot that is located somewhere in a room
▶ The room is modeled as an m x n binary grid where 0 represents a wall and 1 represents an empty slot
▶ The robot starts at an unknown location where is in empty slot
▶ You can move the robot using the given API Robot
▶ The robot with the four given APIs can move forward, turn left, or turn right. Each turn is 90 degrees
▶ When the robot tries to move into a wall cell, its bumper sensor detects the obstacle, and it stays on the cell
▶ Design the algorithm to clean the entire room using the following APIs
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();
}
◈ Constraints
- m == room.length
- n == room[i].length
- 1 <= m <= 100
- 1 <= n <= 200
- room[i][j] is either 0 or 1.
- 0 <= row < m
- 0 <= col < n
- room[row][col] == 1
- All the empty cells can be visited from the starting position.
◈ Input - 1
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
◈ Output - 1
Robot cleaned all rooms.
◈ Input - 2
Input: room = [[1]], row = 0, col = 0
◈ Output - 2
Robot cleaned all rooms.
◎ HOW TO SOLVE IT
▶ Robot has to clean all rooms == robot has to go each cell only once
▶ Make the algorithms using backtracking to clean every cell
▶ The robot can only turn 90 degrees => set the searching direction to east => south => west => north
▶ Create the class 'Point' which has row and col
▶ Use the Hashset<Point> to check a cell has been visited during the backtracking
( can't use the array since can't define the size )
▶ Consider not only points but also robots after cleaning in your backtracking algorithms
/**
* // This is the robot's control interface.
* // You should not implement it, or speculate about its implementation
* interface Robot {
* // Returns true if the cell in front is open and robot moves into the cell.
* // Returns false if the cell in front is blocked and robot stays in the current cell.
* public boolean move();
*
* // Robot will stay in the same cell after calling turnLeft/turnRight.
* // Each turn will be 90 degrees.
* public void turnLeft();
* public void turnRight();
*
* // Clean the current cell.
* public void clean();
* }
*/
class Point {
int m_nRow;
int m_nCol;
Point(int nRow, int nCol){
m_nRow = nRow;
m_nCol = nCol;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return m_nRow == point.m_nRow && m_nCol == point.m_nCol;
}
@Override
public int hashCode() {
return Objects.hash(m_nRow, m_nCol);
}
}
class Solution {
Set<Point> visitP = new HashSet<>();
// east, south, west , north
static int[] dr = {0,1,0, -1};
static int[] dc = {1,0,-1, 0};
public void cleanRoom(Robot robot) {
clean(robot, 0, 0, 0);
}
public void clean(Robot robot, int nRow, int nCol, int nDirection) {
// do the clean
robot.clean();
visitP.add(new Point(nRow, nCol));
for(int k = 0; k < 4; k++) {
int nSearchD = (nDirection + k);
if(nSearchD > 3)
nSearchD -= 4;
int nSearchRow = nRow + dr[nSearchD];
int nSearchCol = nCol + dc[nSearchD];
if(!visitP.contains(new Point(nSearchRow, nSearchCol)) && robot.move()) {
System.out.println(nSearchRow + " / " + nSearchCol);
clean(robot, nSearchRow, nSearchCol, nSearchD);
// the robot has to go back with the point
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnRight();
robot.turnRight();
}
robot.turnRight();
}
}
}
I HOPE YOUR DAY GOES WELL :)
'※ 릿코드 ( LeetCode ) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[Hard] 1293. Shortest Path in a Grid with Obstacles Elimination (Java) (2) | 2023.12.17 |
---|---|
[Hard] 2534. Time Taken to Cross the Door(Java) (2) | 2023.12.15 |
[Medium] 419. Battleships in a Board (Java) (3) | 2023.12.05 |
[Medium] 366. Find Leaves of Binary Tree (Java) (0) | 2023.12.04 |
[Easy] 191. Number of 1Bits (Java) (0) | 2023.11.29 |