Go 实现广度优先搜索
广度优先搜索
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 1 | 0 |
2 | 0 | 1 | 0 | 1 | 0 |
3 | 1 | 1 | 1 | 0 | 0 |
4 | 0 | 1 | 0 | 0 | 1 |
5 | 0 | 1 | 0 | 0 | 0 |
按照“上左下右”的顺序进行探索。
0-(0, 0)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | |||
1 | 1 | ||||
2 | 1 | 1 | |||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(1, 0),$\varnothing$}
$\text {Q}$:(1, 0)
1-(1, 0)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | |||
1 | 1 | 1 | |||
2 | 1 | 1 | |||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(2, 0),(1, 1)}
$\text {Q}$:(2, 0),(1, 1)
2-(2, 0)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | |||
1 | 1 | 1 | |||
2 | 2 | 1 | 1 | ||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,$\varnothing$,$\varnothing$}
$\text {Q}$:(1, 1)
2-(1, 1)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | |||
1 | 1 | 2 | 1 | ||
2 | 2 | 1 | 1 | ||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,$\varnothing$,(1, 2)}
$\text {Q}$:(1, 2)
3-(1, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | |||
1 | 1 | 2 | 3 | 1 | |
2 | 2 | 1 | 1 | ||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{(0, 2),$\varnothing$,(2, 2),$\varnothing$}
$\text {Q}$:(0, 2),(2, 2)
4-(0, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | ||
1 | 1 | 2 | 3 | 1 | |
2 | 2 | 1 | 1 | ||
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,$\varnothing$,(0, 3)}
$\text {Q}$:(2, 2),(0, 3)
4-(2, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | ||
1 | 1 | 2 | 3 | 1 | |
2 | 2 | 1 | 4 | 1 | |
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,$\varnothing$,$\varnothing$}
$\text {Q}$:(0, 3)
5-(0, 3)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | |
1 | 1 | 2 | 3 | 1 | |
2 | 2 | 1 | 4 | 1 | |
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,$\varnothing$,(0, 4)}
$\text {Q}$:(0, 4)
6-(0, 4)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | |
2 | 2 | 1 | 4 | 1 | |
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(1, 4),$\varnothing$}
$\text {Q}$:(1, 4)
7-(1, 4)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | |
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(2, 4),$\varnothing$}
$\text {Q}$:(2, 4)
8-(2, 4)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | ||
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(3, 4),$\varnothing$}
$\text {Q}$:(3, 4)
9-(3, 4)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 9 | |
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,(3, 3),$\varnothing$,$\varnothing$}
$\text {Q}$:(3, 3)
10-(3, 3)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 1 | |||
5 | 1 |
{$\varnothing$,$\varnothing$,(4, 3),$\varnothing$}
$\text {Q}$:(4, 3)
11-(4, 3)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 11 | 1 | ||
5 | 1 |
{$\varnothing$,(4, 2),(5, 3),$\varnothing$}
$\text {Q}$:(4, 2),(5, 3)
12-(4, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 12 | 11 | 1 | |
5 | 1 |
{$\varnothing$,$\varnothing$,(5, 2),$\varnothing$}
$\text {Q}$:(5, 3),(5, 2)
12-(5, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 12 | 11 | 1 | |
5 | 1 | 12 |
{$\varnothing$,$\varnothing$,$\varnothing$,(5, 4)}
$\text {Q}$:(5, 2),(5, 4)
13-(5, 2)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 12 | 11 | 1 | |
5 | 1 | 13 | 12 |
{$\varnothing$,$\varnothing$,$\varnothing$,$\varnothing$}
$\text {Q}$:(5, 4)
13-(5, 4)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 1 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 1 | 7 |
2 | 2 | 1 | 4 | 1 | 8 |
3 | 1 | 1 | 1 | 10 | 9 |
4 | 1 | 12 | 11 | 1 | |
5 | 1 | 13 | 12 | 13 |
源代码
$\text {mazi.in}$:
1 | 6 5 |
$\text {maze.go}$:
1 | package main |
0 1 0 0 0
0 0 0 1 0
0 1 0 1 0
1 1 1 0 0
0 1 0 0 1
0 1 0 0 00 0 4 5 6
1 2 3 0 7
2 0 4 0 8
0 0 0 10 9
0 0 12 11 0
0 0 13 12 13