当前位置:网站首页>February 15, 2022: sweeping robot. There is a floor sweeping robot in the room (represented by a grid). Each grid in the grid has two possibilities: empty and obstacles. The sweeping robot provides fo
February 15, 2022: sweeping robot. There is a floor sweeping robot in the room (represented by a grid). Each grid in the grid has two possibilities: empty and obstacles. The sweeping robot provides fo
2022-07-01 19:43:00 【Fuda frame constructor daily question】
2022-02-15: Sweeping robot .
room ( Use a grid to represent ) There is a sweeping robot in the .
Each grid in the grid has two possibilities: empty and obstacle .
The sweeping robot provides 4 individual API, Can move forward , Turn left or right . Every turn 90 degree .
When the sweeping robot tries to enter the obstacle grid , Its collision sensor will detect obstacles , Keep it where it is .
Please use the provided 4 individual API Write an algorithm to let the robot clean the whole room .
interface Robot {
// If the next box is empty , Then return to true, And move to this square
// If the next box is an obstacle , Then return to false, And stay where you are
boolean move();
// Calling turnLeft/turnRight After that, the robot will stay in its original position
// Every turn 90 degree
void turnLeft();
void turnRight();
// Clear the square
void clean();
}
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
analysis :
For room grille 0 or 1 fill .0 It means an obstacle ,1 Means that it can be passed .
Robot from row=1,col=3 Starting from the initial position of . Below the line in the upper left corner , Three columns to the right .
Be careful :
Input is only used to initialize the position of the room and robot . You need “ Blind solution ” This problem .
In other words , You have to know nothing about the room and the location of the robot , Use only 4 A given API solve the problem .
The initial position of the sweeping robot must be an open space .
The initial direction of the sweeping robot is upward .
All accessible grids are connected , That is, all are marked as 1 All grid robots can reach .
It can be assumed that the grid is surrounded by walls .
Power button 489.
answer 2022-02-15:
map Record the road .
0 On ,1 Right ,2 Next ,3 Left .
recursive .
The code to use golang To write . The code is as follows :
package main
import "strconv"
func main() {
}
// Do not submit the contents of this interface
type Robot interface {
move() bool
turnLeft()
turnRight()
clean()
}
// Submit the following
func cleanRoom(robot Robot) {
clean(robot, 0, 0, 0, make(map[string]struct{
}))
}
var ds = [][]int{
{
-1, 0}, {
0, 1}, {
1, 0}, {
0, -1}}
// robot robot,
// Where you are now (x,y), And I haven't been here before
// In what direction does the robot face d,0 1 2 3
// visited It records where the robot walked
// Function functions : Don't repeat visited The location inside , Put the rest of the position , Clean it up !
// And going back !
func clean(robot Robot, x, y, d int, visited map[string]struct{
}) {
robot.clean()
visited[strconv.Itoa(x)+"_"+strconv.Itoa(y)] = struct{
}{
}
for i := 0; i < 4; i++ {
// d = 0 : 0 1 2 3
// d = 1 : 1 2 3 0
// d = 2 : 2 3 0 1
// d = 3 : 3 0 1 2
// The next step !
nd := (i + d) % 4
// The direction of the next step is set ! Where is the next step ?(nx, ny)
nx := ds[nd][0] + x
ny := ds[nd][1] + y
_, ok := visited[strconv.Itoa(nx)+"_"+strconv.Itoa(ny)]
if !ok && robot.move() {
clean(robot, nx, ny, nd, visited)
}
robot.turnRight()
}
// Responsible for going back : Previous position , How to get to you ! You're going back , And the direction is the same as before you , Should agree !
robot.turnRight()
robot.turnRight()
robot.move()
robot.turnRight()
robot.turnRight()
}
边栏推荐
猜你喜欢

商业智能BI开发和报表开发有什么本质区别?

如何正确使用Vertx操作Redis(3.9.4带源码分析)

SIP protocol of gb28181

JS 之 常用内置类的使用
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat

A brief understanding of white box encryption technology

Basic use of MySQL

After studying 11 kinds of real-time chat software, I found that they all have these functions

Solution and summary of Nacos startup failure

SQL 入门计划-1-选择
随机推荐
今日群里分享的面试题
uni-app微信小程序一键登录获取权限功能
较真儿学源码系列-InheritableThreadLocal(逐行源码带你分析作者思路)
[Mori city] random talk on GIS data (I)
IPv4地址、子网掩码、网关
JS ternary expression complex condition judgment
2022/6/8-2022/6/12
Introduction and installation of crunch, and making password dictionary with crunch
Mysql查询结果去除换行
GC garbage collection
tensorflow报错Could not load dynamic library ‘libcudnn.so.8
Transaction isolation level gap lock deadlock
mysql 報錯 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
703. The k-th element in the data flow
MySQl的基本使用
直播HLS协议
Solution and summary of Nacos startup failure
What must be done in graduation season before going to Shanhai
新窗口打开页面-window.open
Collect Tiktok video