Skip to content

Commit 63f5514

Browse files
committed
add depthFirst && breadthFirst
1 parent 1c757bc commit 63f5514

8 files changed

Lines changed: 315 additions & 0 deletions

File tree

breadthFirst/breadthFirst.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type node struct {
8+
x int
9+
y int
10+
f int
11+
s int
12+
}
13+
14+
func main() {
15+
var que [2501]node
16+
var a, book [51][51]int
17+
var next = [4][2]int{{0, 1},
18+
{1, 0},
19+
{0, -1},
20+
{-1, 0},
21+
}
22+
23+
var head, tail int
24+
25+
var n, m int
26+
fmt.Println("input the n×m")
27+
fmt.Scanf("%d %d", &n, &m)
28+
for i := 1; i <= n; i++ {
29+
for j := 1; j <= m; j++ {
30+
fmt.Scanf("%d", &a[i][j])
31+
}
32+
}
33+
34+
var startX, startY, p, q int
35+
fmt.Scanf("%d %d %d %d", &startX, &startY, &p, &q)
36+
37+
head = 1
38+
tail = 1
39+
40+
que[tail].x = startX
41+
que[tail].y = startY
42+
que[tail].f = 0
43+
que[tail].s = 0
44+
tail++
45+
book[startX][startY] = 1
46+
47+
var flag = 0
48+
var tx, ty int
49+
for head < tail {
50+
for i := 0; i <= 3; i++ {
51+
tx = que[head].x + next[i][0]
52+
ty = que[head].y + next[i][1]
53+
54+
if tx < 1 || tx > n || ty < 1 || ty > m {
55+
continue
56+
}
57+
58+
if a[tx][ty] == 0 && book[tx][ty] == 0 {
59+
book[tx][ty] = 1
60+
que[tail].x = tx
61+
que[tail].y = ty
62+
que[tail].f = head
63+
que[tail].s = que[head].s + 1
64+
tail++
65+
}
66+
67+
if tx == p && ty == q {
68+
flag = 1
69+
break
70+
}
71+
}
72+
73+
if flag == 1 {
74+
break
75+
}
76+
head++
77+
}
78+
79+
fmt.Print(que[tail-1].s)
80+
}

breadthFirst2/Searcher.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"strings"
6+
)
7+
8+
type node struct {
9+
pt point
10+
steps int
11+
father int
12+
}
13+
14+
var directions = [4][2]int{
15+
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
16+
}
17+
18+
type Searcher struct {
19+
StartPt point
20+
book []point
21+
m *Map
22+
queue []node
23+
head, tail int
24+
}
25+
26+
func NewSearcher(m *Map, pt point) *Searcher {
27+
book := make([]point, 1, 16)
28+
book[0] = pt
29+
queue := make([]node, 1, 16)
30+
queue[0].pt = pt
31+
32+
return &Searcher{StartPt: pt, book: book, m: m, queue: queue, tail: 1}
33+
}
34+
35+
func (s Searcher) IsWalked(pt point) bool {
36+
for _, v := range s.book {
37+
if v == pt {
38+
return true
39+
}
40+
}
41+
return false
42+
}
43+
44+
func (s Searcher) Path() string {
45+
res := make([]string, 0, 16)
46+
for i := len(s.queue) - 1; i != 0; i = s.queue[i].father {
47+
res = append(res, s.queue[i].pt.String())
48+
}
49+
reverse := make([]string, len(res))
50+
var index int
51+
for i := len(res) - 1; i >= 0; i-- {
52+
reverse[index] = res[i]
53+
index++
54+
}
55+
return strings.Join(reverse, " ")
56+
}
57+
58+
func (s *Searcher) Walk() string {
59+
var next point
60+
Label:
61+
for s.head < s.tail {
62+
for _, dir := range directions {
63+
next = s.queue[s.head].pt.walk(dir)
64+
if s.m.Illegal(next) || s.IsWalked(next) {
65+
continue
66+
}
67+
s.book = append(s.book, next)
68+
69+
var newNode node
70+
newNode.pt = next
71+
newNode.father = s.head
72+
newNode.steps = s.queue[s.head].steps + 1
73+
s.queue = append(s.queue, newNode)
74+
s.tail++
75+
76+
if s.m.Arrive(next) {
77+
break Label
78+
}
79+
}
80+
s.head++
81+
}
82+
return s.Path()
83+
}

breadthFirst2/breadthFirst2.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
m := NewMap(5, 4)
9+
searcher := NewSearcher(m, point{0, 0})
10+
fmt.Println(searcher.Walk())
11+
}

breadthFirst2/map.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
type Map struct {
4+
data [][]int
5+
x int
6+
y int
7+
}
8+
9+
func NewMap(n, m int) *Map {
10+
var M Map
11+
M.data = make([][]int, n)
12+
for k, _ := range M.data {
13+
M.data[k] = make([]int, m)
14+
}
15+
16+
M.x = n - 1
17+
M.y = m - 1
18+
M.Init()
19+
return &M
20+
}
21+
22+
func (m *Map) Init() {
23+
m.data[0][2] = -1
24+
m.data[2][2] = -1
25+
m.data[3][1] = -1
26+
m.data[4][3] = -1
27+
m.data[3][3] = 1
28+
}
29+
30+
func (m Map) find(pt point) int {
31+
return m.data[pt.x][pt.y]
32+
}
33+
34+
func (m Map) Illegal(pt point) bool {
35+
return pt.x < 0 || pt.x > m.x || pt.y < 0 ||
36+
pt.y > m.y || m.find(pt) == -1
37+
}
38+
39+
func (m Map) Arrive(pt point) bool {
40+
return m.find(pt) == 1
41+
}

breadthFirst2/map_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestMap(t *testing.T) {
9+
m := NewMap(5, 4)
10+
fmt.Println(m.data)
11+
if m == nil {
12+
t.Error("should not happened")
13+
}
14+
if m.x != 4 || m.y != 3 {
15+
t.Error("5,4", m.y, m.x)
16+
}
17+
18+
if !m.Illegal(point{-1, 0}) || !m.Illegal(point{4, 3}) {
19+
t.Error("illegal but not")
20+
}
21+
22+
if m.Arrive(point{2, 3}) {
23+
t.Error("illegal but not")
24+
}
25+
if !m.Arrive(point{3, 3}) {
26+
t.Error("illegal but not")
27+
}
28+
}

breadthFirst2/pt.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type point struct {
8+
x, y int
9+
}
10+
11+
func (p point) walk(dir [2]int) (next point) {
12+
next.x = p.x + dir[0]
13+
next.y = p.y + dir[1]
14+
return next
15+
}
16+
17+
func (p point) String() string {
18+
return fmt.Sprintf("(%d,%d)", p.x, p.y)
19+
}

breadthFirst2/searcher_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSearcher(t *testing.T) {
9+
m := NewMap(5, 4)
10+
s := NewSearcher(m, point{0, 0})
11+
fmt.Println(s.Walk())
12+
}

depthFirst/depthFirst.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var (
8+
n int
9+
a []int
10+
book []int
11+
)
12+
13+
func dfs(step int) {
14+
if step > n {
15+
for i := 1; i <= n; i++ {
16+
fmt.Print(a[i])
17+
}
18+
fmt.Println("")
19+
return
20+
}
21+
for i := 1; i <= n; i++ {
22+
if book[i] == 0 {
23+
a[step] = i
24+
book[i] = 1
25+
26+
dfs(step + 1)
27+
book[i] = 0
28+
}
29+
}
30+
}
31+
32+
func main() {
33+
fmt.Println("请输入n的值")
34+
fmt.Scanf("%d", &n)
35+
fmt.Println("n:", n)
36+
a = make([]int, n+1)
37+
book = make([]int, n+1)
38+
fmt.Println(a)
39+
40+
dfs(1)
41+
}

0 commit comments

Comments
 (0)