@@ -9,7 +9,7 @@ type sliceOfMatrix []matrix
9
9
var inputPuzzle matrix
10
10
11
11
// the final required output puzzle
12
- var solvedPuzzle matrix
12
+ var outputPuzzle matrix
13
13
14
14
// the steps taken to reach from scrambled puzzle to required output puzzle
15
15
var solvedSteps sliceOfMatrix
@@ -21,7 +21,7 @@ var heap []*Node
21
21
// value is the cost of the last matrix in path
22
22
// path is the list of matrices which represents the traversal (steps) done from the initial matrix
23
23
// level represents the depth at which the current (last matrix from path) state is
24
- // blankCell represents the row and column in which the blank cell was in the parent state
24
+ // parentBlankCell represents the row and column in which the blank cell was in the parent state
25
25
type Node struct {
26
26
value int
27
27
path sliceOfMatrix
@@ -30,10 +30,20 @@ type Node struct {
30
30
}
31
31
32
32
func init () {
33
- solvedPuzzle = [][] int {[] int { 1 , 2 , 3 }, [] int { 8 , 0 , 4 }, [] int { 7 , 6 , 5 }}
33
+ outputPuzzle = make ( matrix , 3 )
34
34
solvedSteps = make (sliceOfMatrix , 0 )
35
35
heap = make ([]* Node , 0 )
36
36
inputPuzzle = make (matrix , 3 )
37
+
38
+ // initialize outputPuzzle
39
+ for i := range outputPuzzle {
40
+ outputPuzzle [i ] = make ([]int , 3 )
41
+ for j := range outputPuzzle [i ] {
42
+ outputPuzzle [i ][j ] = 0
43
+ }
44
+ }
45
+
46
+ // initialize inputPuzzle
37
47
for i := range inputPuzzle {
38
48
inputPuzzle [i ] = make ([]int , 3 )
39
49
for j := range inputPuzzle [i ] {
@@ -47,7 +57,23 @@ func main() {
47
57
fmt .Println ("\n -- 8 Puzzle problem using A* Algorithm --" )
48
58
fmt .Println ("\n -- Note: For blank cell use '0' --" )
49
59
50
- fmt .Println ("\n Start entering puzzle values:" )
60
+ fmt .Println ("\n Start entering desired 3x3 output puzzle:" )
61
+ for i := range outputPuzzle {
62
+ for j := range outputPuzzle [i ] {
63
+ value := - 1
64
+ fmt .Print ("Enter for " , i + 1 , j + 1 , ": " )
65
+ fmt .Scanf ("%d\n " , & value )
66
+ outputPuzzle [i ][j ] = value
67
+ }
68
+ fmt .Println ("" )
69
+ }
70
+
71
+ if ! outputPuzzle .isValid () {
72
+ fmt .Println ("\n -- Invalid output puzzle. --" )
73
+ return
74
+ }
75
+
76
+ fmt .Println ("\n Start entering 3x3 scrambled input puzzle:" )
51
77
for i := range inputPuzzle {
52
78
for j := range inputPuzzle [i ] {
53
79
value := - 1
@@ -58,7 +84,11 @@ func main() {
58
84
fmt .Println ("" )
59
85
}
60
86
61
- // print the puzzle
87
+ // print the desired output puzzle
88
+ fmt .Println ("\n Your desired output puzzle is:" )
89
+ outputPuzzle .displayMatrix ()
90
+
91
+ // print the input scrambled puzzle
62
92
fmt .Println ("\n Your scrambled puzzle is: " )
63
93
inputPuzzle .displayMatrix ()
64
94
@@ -71,5 +101,6 @@ func main() {
71
101
}
72
102
}
73
103
104
+ // puzzle = input - output
74
105
// puzzle = [][]int{[]int{2, 8, 3}, []int{1, 6, 4}, []int{7, 0, 5}} - [][]int{[]int{1, 2, 3}, []int{8, 0, 4}, []int{7, 6, 5}}
75
- // puzzle : = matrix{[]int{1, 8, 2}, []int{0, 4, 3}, []int{7, 6, 5}} - [][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 0}}
106
+ // puzzle = matrix{[]int{1, 8, 2}, []int{0, 4, 3}, []int{7, 6, 5}} - [][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 0}}
0 commit comments