-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
91 lines (74 loc) · 1.44 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package yuru
import (
"fmt"
"bytes"
)
type State struct {
combo int
nowR int
nowC int
route Route
G Board
turn int
startR int
startC int
}
type Queue []*State
func NewState(r, c, turn int, route Route,P Board,conf *Config) *State {
s := State{
nowR: r,
nowC: c,
turn: turn,
}
s.combo = count(P,conf)
if route == nil {
s.route = make(Route, 0)
} else {
s.route = route
}
s.G = P
return &s
}
func (s *State) Max(max int) bool {
return max == s.combo
}
func (s *State) Less(t *State) bool {
if s.combo > t.combo {
return true
} else if s.combo == t.combo && s.turn < t.turn {
return true
}
return false
}
func (s *State) String() string {
rtn := bytes.NewBuffer(make([]byte,0,200))
rtn.WriteString(fmt.Sprintf("Start(%d,%d)-End(%d,%d)", s.startR+1, s.startC+1, s.nowR+1, s.nowC+1))
rtn.WriteString(fmt.Sprintln())
rtn.WriteString(s.route.String())
rtn.WriteString(fmt.Sprintln())
rtn.WriteString(fmt.Sprintf("combo:%d", s.combo))
rtn.WriteString(fmt.Sprintln())
rtn.WriteString(fmt.Sprintln())
rtn.WriteString(s.G.String())
return rtn.String()
}
func (q Queue) Len() int {
return len(q)
}
func (q Queue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}
func (q Queue) Less(i, j int) bool {
return q[i].Less(q[j])
}
func (q *Queue) Pop() interface{} {
old := *q
n := len(old)
item := old[0]
*q = old[1:n]
return item
}
func (q *Queue) Push(x interface{}) {
item := x.(*State)
*q = append(*q, item)
}