2019蓝桥杯大学B组E题
题目描述
<https://blog.csdn.net/weixin_42172261/article/details/89647625
https://blog.csdn.net/a1097304791/article/details/88791546
package main
import (
"fmt"
)
var next [][]int = [][]int{{1, 0}, {0, -1}, {0, 1}, {-1, 0}} //字典序bfs
var path []byte = []byte{'D', 'L', 'R', 'U'}
var maze [100][100]int //迷宫
var book [100][100]int //标记当前是否走过
var n, m int
type node struct {
x, y, step, pre int //pre代表上一步在队列q中的下标
ch byte
}
var q [10000]node //bfs队列,出队不销毁
func getpath(t int) {
if t == 1 {
return
}
getpath(q[t].pre)
fmt.Printf("%c", q[t].ch)
}
func bfs() {
head, tail := 1, 1
q[tail].x, q[tail].y = 0, 0
q[tail].step, q[tail].pre = 0, -1
tail++
book[0][0] = 1
flag := false
for head < tail {
for k := 0; k < 4; k++ {
tx := q[head].x + next[k][0]
ty := q[head].y + next[k][1]
if tx < 0 || tx >= n || ty < 0 || ty >= m || book[tx][ty] != 0 || maze[tx][ty] == 1 {
continue
}
book[tx][ty] = 1
q[tail].x, q[tail].y = tx, ty
q[tail].pre = head
q[tail].step = q[head].step + 1
q[tail].ch = path[k]
tail++
if tx == n-1 && ty == m-1 {
flag = true
break
}
}
if flag == true {
break
}
head++
}
getpath(tail - 1)
}
func main() {
fmt.Scanf("%d %d\n", &n, &m)
for i := 0; i < n; i++ {
var s1 string
fmt.Scanf("%s\n", &s1)
//fmt.Printf(":%s\n", s1)
//fmt.Printf("%d, %s\n", len(s1), s1)
for j := 0; j < m; j++ {
maze[i][j] = int(s1[j] - '0')
}
}
bfs()
}