-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidecost.vbs
More file actions
82 lines (68 loc) · 1.94 KB
/
slidecost.vbs
File metadata and controls
82 lines (68 loc) · 1.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'
' Slide puzzle heuristic cost objects
'
option explicit
class tcosthamming
public function getcost(byval board)
dim i, cost : cost = 0
for i = 0 to ubound(board.m_board)
dim tile : tile = board.m_board(i)
if tile > 0 and tile <> i+1 then
cost = cost+1
end if
next
getcost = cost
end function
end class
class tcostmanhattan
private m_degree
private m_walk
public function init(byval degree)
if degree < 2 or degree > 5 then
m_degree = 3
else
m_degree = degree
end if
m_walk = makewalk(degree)
set init = me
end function
public function getcost(byval board)
dim i, cost : cost = 0
for i = 0 to m_degree*m_degree-1
cost = cost + m_walk(board.m_board(i), i)
next
getcost = cost
end function
private function makewalk(degree)
dim i, j, arr()
redim arr(degree*degree-1, degree*degree-1)
for i = 0 to degree*degree-1
for j = 1 to degree*degree
if i = 0 then
arr(i, j-1) = 0
else
arr(i, j-1) = abs(getrow(i-1) - getrow(j-1)) + abs(getcol(i-1) - getcol(j-1))
end if
next
next
makewalk = arr
end function
private function getrow(byval n)
getrow = n \ m_degree
end function
private function getcol(byval n)
getcol = n mod m_degree
end function
private function lpad(byval istr, byval length, byval padchar)
lpad = right(string(length, padchar) & istr, length)
end function
public sub printwalk
dim i, j
for i = 0 to m_degree*m_degree-1
for j = 0 to m_degree*m_degree-1
wscript.stdout.write lpad(m_walk(i, j), 3, " ")
next
wscript.stdout.writeline
next
end sub
end class