This repository was archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheuristics.go
More file actions
60 lines (48 loc) · 1.36 KB
/
Copy pathheuristics.go
File metadata and controls
60 lines (48 loc) · 1.36 KB
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
package main
func abs(x int8) uint16 {
if x < 0 {
return uint16(-x)
}
return uint16(x)
}
func linearConflictCell(value uint16, x, y uint8, board *Board, goal []Cell) uint16 {
distance := manhattanCell(value, x, y, board, goal)
for _, n := range board.cells[x][y+1:] {
if n != 0 && goal[n].x == goal[value].x && goal[n].y < goal[value].y {
distance += 2
}
}
for i := x + 1; i < size; i++ {
n := board.cells[i][y]
if n != 0 && goal[n].y == goal[value].y && goal[n].x < goal[value].x {
distance += 2
}
}
return distance
}
func manhattanCell(value uint16, x, y uint8, board *Board, goal []Cell) uint16 {
cell := goal[value]
var distance uint16
distance += abs(int8(cell.x - x))
distance += abs(int8(cell.y - y))
return distance
}
func hammingCell(value uint16, x, y uint8, board *Board, goal []Cell) uint16 {
cell := goal[value]
if cell.x == x && cell.y == y {
return 0
} else {
return 1
}
}
func (b *Board) priority(goal []Cell) uint16 {
var distance uint16
for i, n := range b.cells {
for j, m := range n {
if m != 0 {
distance += method(m, uint8(i), uint8(j), b, goal) // method is a pointer to one of the functions up here
}
}
}
return distance
}