当前位置:网站首页>January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
2022-06-23 02:42:00 【Fuda scaffold constructor's daily question】
2022-01-31: maze III.
There is a ball in the maze of open spaces and walls . The ball can go up (u) Next (d) Left (l) Right (r) Scroll in four directions , But it doesn't stop rolling until it hits a wall . When the ball stops , You can select the next direction . There is also a hole in the maze , When the ball moves through the hole , Will fall into the hole .
Given the starting position of the ball , Destination and maze , Find the path to let the ball fall into the hole at the shortest distance . The definition of distance is that the ball starts from its starting position ( barring ) Destination ( Include ) The number of open spaces passed . adopt 'u', 'd', 'l' and 'r' Output the moving direction of the ball . Since there may be multiple shortest paths , Please output the path with the smallest dictionary order . If the ball doesn't get into the hole , Output "impossible".
The maze consists of a 0 and 1 Two dimensional array representation of . 1 Wall ,0 Representing vacant land . You can assume that the edges of the maze are all walls . The coordinates of the starting position and destination are given by row number and column number .
Power button 499.
answer 2022-01-31:
Breadth first traversal . Every step , All need to be recorded .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
maze := [][]int{
{0, 0, 0, 0, 0},
{1, 1, 0, 0, 1},
{0, 0, 0, 0, 0},
{0, 1, 0, 0, 1},
{0, 1, 0, 0, 0},
}
ball := []int{4, 3}
hole := []int{0, 1}
ret := findShortestWay(maze, ball, hole)
fmt.Println(ret)
}
// node : Where have you come ?(r,c) This position
// From which direction !d -> 0 1 2 3 4
// What decision did you make before you came to this position .
type Node struct {
r int
c int
d int
p string
}
func NewNode(row, col, dir0 int, path0 string) *Node {
ans := &Node{}
ans.r = row
ans.c = col
ans.d = dir0
ans.p = path0
return ans
}
func findShortestWay(maze [][]int, ball, hole []int) string {
n := len(maze)
m := len(maze[0])
q1 := make([]*Node, n*m)
q2 := make([]*Node, n*m)
s1 := 0
s2 := 0
visited := make([][][]bool, len(maze))
for i := 0; i < len(maze); i++ {
visited[i] = make([][]bool, len(maze[0]))
for j := 0; j < len(maze[0]); j++ {
visited[i][j] = make([]bool, 4)
}
}
s1 = spread(maze, n, m, NewNode(ball[0], ball[1], 4, ""), visited, q1, s1)
for s1 != 0 {
for i := 0; i < s1; i++ {
cur := q1[i]
if hole[0] == cur.r && hole[1] == cur.c {
return cur.p
}
s2 = spread(maze, n, m, cur, visited, q2, s2)
}
tmp := q1
q1 = q2
q2 = tmp
s1 = s2
s2 = 0
}
return "impossible"
}
var to = [][]int{{1, 0}, {0, -1}, {0, 1}, {-1, 0}, {0, 0}}
var re = []string{"d", "l", "r", "u"}
// maze maze , Go to the grid
// n Row number
// m Number of columns
// The current node ,cur -> (r,c) Direction route ( decision )
// v [ That's ok ][ Column ][ Direction ] A grid , In fact, in the case of finite width traversal , yes 4 A little bit !
// q The next level of queues
// s Where is the next level of queue filled ,size
// Current point cur, The split split split , It's time to keep going , The resulting point of a lower layer , Get into q,s++
// Return value :q Where has it grown ? return size -> s
func spread(maze [][]int, n, m int, cur *Node, v [][][]bool, q []*Node, s int) int {
d := cur.d
r := cur.r + to[d][0]
c := cur.c + to[d][1]
// Cleavage !
if d == 4 || r < 0 || r == n || c < 0 || c == m || maze[r][c] != 0 {
for i := 0; i < 4; i++ {
if i != d {
r = cur.r + to[i][0]
c = cur.c + to[i][1]
if r >= 0 && r < n && c >= 0 && c < m && maze[r][c] == 0 && !v[r][c][i] {
v[r][c][i] = true
next := NewNode(r, c, i, cur.p+re[i])
q[s] = next
s++
}
}
}
} else { // Not split ! Continue to go !
if !v[r][c][d] {
v[r][c][d] = true
q[s] = NewNode(r, c, d, cur.p)
s++
}
}
return s
}The results are as follows :
边栏推荐
- Markdown - mark above / below symbol (typora, latex)
- 2022-02-05: the k-th decimal number of dictionary order. Given integers n and K, find 1
- February 3, 2022: a group of people (two or more) want to meet at the same place
- Gorilla/mux framework (RK boot): add swagger UI
- 8 vertical centering methods
- Hypervisor Necromancy; Recover kernel protector (2)
- Spread spectrum and frequency hopping
- How PHP uses redis
- [target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
- Solve the problem that QQ flash photos cannot be saved
猜你喜欢

C language series - Section 4 - arrays

5g access network and base station evolution

How to make word notes beautiful

6. template for integer and real number dichotomy

Interviewer: with the for loop, why do you need foreach??

Microservice Optimization: internal communication of microservices using grpc

Small knowledge points of asset

Vulnhub DC-5

Circuit analysis (circuit principle)

My good brother gave me a difficult problem: retry mechanism
随机推荐
Spread spectrum and frequency hopping
My good brother gave me a difficult problem: retry mechanism
Record a penetration caused by log4j
6. template for integer and real number dichotomy
Wechat applet camera compressed image is Base64
Markdown - mark above / below symbol (typora, latex)
Xgboost Guide
How to make a borrowing card
No error is reported when using the Gorm framework to create a table, but the data cannot be inserted successfully
Special exercise split line-----------------------------
JS request path console reports an error failed to launch 'xxx' because the scheme does not have a registered handler
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Supervisor multi process management exception automatic restart visual management
How to use pictures in Excel in PPT template
Digital circuit logic design
5. concept of ruler method
February 2, 2022: the closest binary search tree value II. Given a non empty two
Using mock data in vite projects -vite plugin mock
Pywebio to quickly build web applications
Understand GB, gbdt and xgboost step by step