手动走迷宫
命令行输入方向,控制1到迷宫出口
package main
import "fmt"
const M
, N
= 11, 10
var ymove
, xmove
int = 0, 0
var data
[M
][N
]int =[M
][N
]int{
{ 1, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 2, 0, 0, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 2, 0, 0, 2, 2, 0 },
{ 0, 0, 0, 0, 2, 0, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 2, 0, 0, 0, 0, 2 },
{ 2, 2, 2, 0, 0, 2, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 2, 0, 2 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 2, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
}
func Show() {
fmt
.Println("-----------------------------------------------")
for i
:= 0; i
< M
; i
++{
for j
:= 0; j
< N
; j
++ {
fmt
.Printf("%5d", data
[i
][j
])
}
fmt
.Println()
}
}
func run(direct
string) {
if direct
== "s"{
if ymove
- 1 >= 0 && data
[ymove
- 1][xmove
] != 2 {
data
[ymove
- 1][xmove
], data
[ymove
][xmove
] = data
[ymove
][xmove
], data
[ymove
- 1][xmove
]
ymove
--
}
}else if (direct
== "x"){
if ymove
+ 1 <= M
- 1 && data
[ymove
+ 1][xmove
] != 2 {
data
[ymove
+ 1][xmove
], data
[ymove
][xmove
] = data
[ymove
][xmove
], data
[ymove
+ 1][xmove
]
ymove
++
}
}else if (direct
== "z"){
if xmove
- 1 >= 0 && data
[ymove
][xmove
- 1] != 2 {
data
[ymove
][xmove
- 1], data
[ymove
][xmove
] = data
[ymove
][xmove
], data
[ymove
][xmove
- 1]
xmove
--
}
}else if(direct
== "y"){
if xmove
+ 1 <= N
- 1 && data
[ymove
][xmove
+ 1] != 2 {
data
[ymove
][xmove
+ 1], data
[ymove
][xmove
] = data
[ymove
][xmove
], data
[ymove
][xmove
+ 1]
xmove
++
}
}else{
fmt
.Println("不知道的方向,不可以走")
}
}
func main() {
Show()
for {
var str
string
fmt
.Scanln(&str
)
run(str
)
Show()
}
}
走迷宫自动
相对于当前位置,一般有四个方向走,因为我们要从左上角 —》 右下角。 因此规定优先“右”, “下”, “上”, “左”迷宫可以看成是一棵四叉树。相当于四叉树的深度遍历。递归可以用栈实现
递归
package main
import (
"fmt"
)
const M
, N
= 10, 10
var cangoout
bool = false
var data
[M
][N
]int =[M
][N
]int{
{ 1, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 2, 0, 0, 2, 0, 0, 0 },
{ 2, 0, 0, 0, 2, 0, 0, 2, 2, 0 },
{ 0, 2, 0, 0, 2, 0, 2, 0, 0, 0 },
{ 0, 2, 0, 2, 2, 0, 0, 0, 0, 2 },
{ 2, 2, 2, 2, 2, 2, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 2, 0, 2 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 2, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
}
func Show() {
fmt
.Println("-----------------------------------------------")
for i
:= 0; i
< M
; i
++{
for j
:= 0; j
< N
; j
++ {
fmt
.Printf("%5d", data
[i
][j
])
}
fmt
.Println()
}
}
func FoundOut(imove
int, jmove
int) {
data
[imove
][jmove
] = 3
Show()
if imove
== M
- 1 && jmove
== N
- 1 {
cangoout
= true
return
}else{
if jmove
+ 1 < N
&& data
[imove
][jmove
+ 1] == 0 && cangoout
== false {
FoundOut(imove
, jmove
+ 1)
}
if imove
+ 1 < M
&& data
[imove
+ 1][jmove
] == 0 && cangoout
== false {
FoundOut(imove
+ 1, jmove
)
}
if imove
- 1 > -1 && data
[imove
- 1][jmove
] == 0 && cangoout
== false {
FoundOut(imove
- 1, jmove
)
}
if jmove
- 1 > -1 && data
[imove
][jmove
- 1] == 0 && cangoout
== false {
FoundOut(imove
, jmove
- 1)
}
}
}
func main() {
FoundOut(0, 0)
Show()
fmt
.Println(cangoout
)
}
(0, 0)出栈后,整个递归过程也结束了。
此时canout = false,也就是迷宫是不能解决的
版本二
从上面我们可以看出,递归函数的调用相当于压栈,出栈,因此我们也可以把上面改成栈实现
实现栈
实现栈
栈走迷宫
package main
import (
"fmt"
)
const M
, N
= 10, 10
var data
[M
][N
]int =[M
][N
]int{
{ 1, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 2, 0, 0, 2, 0, 0, 0 },
{ 2, 0, 0, 0, 2, 0, 0, 2, 2, 0 },
{ 0, 2, 0, 0, 2, 0, 2, 0, 0, 0 },
{ 0, 2, 0, 2, 2, 0, 0, 0, 0, 2 },
{ 2, 2, 2, 2, 2, 2, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 2, 0, 2 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 2, 2, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
}
func Show() {
fmt
.Println("-----------------------------------------------")
for i
:= 0; i
< M
; i
++{
for j
:= 0; j
< N
; j
++ {
fmt
.Printf("%5d", data
[i
][j
])
}
fmt
.Println()
}
}
type Pos
struct {
imove
, jomve
int
}
func FoundOut() bool {
stack
:= NewStack()
p
:= Pos
{
imove
: 0,
jomve
: 0,
}
_ = stack
.Push(p
)
for !stack
.IsEmpty() {
Show()
pop
, _ := stack
.Peek()
p
:= (pop
).(Pos
)
data
[p
.imove
][p
.jomve
] = 3
if p
.jomve
== N
- 1 && p
.imove
== M
- 1 {
return true
}
if p
.jomve
+ 1 < N
&& data
[p
.imove
][p
.jomve
+ 1] == 0 {
_ = stack
.Push(Pos
{
imove
: p
.imove
,
jomve
: p
.jomve
+ 1,
})
continue
}
if p
.imove
+ 1 < M
&& data
[p
.imove
+ 1][p
.jomve
] == 0 {
_ = stack
.Push(Pos
{
imove
: p
.imove
+ 1,
jomve
: p
.jomve
,
})
continue
}
if p
.imove
- 1 > -1 && data
[p
.imove
- 1][p
.jomve
] == 0 {
_ = stack
.Push(Pos
{
imove
: p
.imove
- 1,
jomve
: p
.jomve
,
})
continue
}
if p
.jomve
- 1 > -1 && data
[p
.imove
][p
.jomve
- 1] == 0 {
_ = stack
.Push(Pos
{
imove
: p
.imove
,
jomve
: p
.jomve
- 1,
})
continue
}
stack
.Pop()
}
return false
}
func main() {
FoundOut()
Show()
}