Skip to content

Commit 37d493e

Browse files
author
computer0101
committed
ask for desired output puzzle
1 parent 3070a44 commit 37d493e

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

algorithms/a_star/8_puzzle/8_puzzle.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type sliceOfMatrix []matrix
99
var inputPuzzle matrix
1010

1111
// the final required output puzzle
12-
var solvedPuzzle matrix
12+
var outputPuzzle matrix
1313

1414
// the steps taken to reach from scrambled puzzle to required output puzzle
1515
var solvedSteps sliceOfMatrix
@@ -21,7 +21,7 @@ var heap []*Node
2121
// value is the cost of the last matrix in path
2222
// path is the list of matrices which represents the traversal (steps) done from the initial matrix
2323
// 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
2525
type Node struct {
2626
value int
2727
path sliceOfMatrix
@@ -30,10 +30,20 @@ type Node struct {
3030
}
3131

3232
func init() {
33-
solvedPuzzle = [][]int{[]int{1, 2, 3}, []int{8, 0, 4}, []int{7, 6, 5}}
33+
outputPuzzle = make(matrix, 3)
3434
solvedSteps = make(sliceOfMatrix, 0)
3535
heap = make([]*Node, 0)
3636
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
3747
for i := range inputPuzzle {
3848
inputPuzzle[i] = make([]int, 3)
3949
for j := range inputPuzzle[i] {
@@ -47,7 +57,23 @@ func main() {
4757
fmt.Println("\n-- 8 Puzzle problem using A* Algorithm --")
4858
fmt.Println("\n-- Note: For blank cell use '0' --")
4959

50-
fmt.Println("\nStart entering puzzle values:")
60+
fmt.Println("\nStart 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("\nStart entering 3x3 scrambled input puzzle:")
5177
for i := range inputPuzzle {
5278
for j := range inputPuzzle[i] {
5379
value := -1
@@ -58,7 +84,11 @@ func main() {
5884
fmt.Println("")
5985
}
6086

61-
// print the puzzle
87+
// print the desired output puzzle
88+
fmt.Println("\nYour desired output puzzle is:")
89+
outputPuzzle.displayMatrix()
90+
91+
// print the input scrambled puzzle
6292
fmt.Println("\nYour scrambled puzzle is: ")
6393
inputPuzzle.displayMatrix()
6494

@@ -71,5 +101,6 @@ func main() {
71101
}
72102
}
73103

104+
// puzzle = input - output
74105
// 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}}

algorithms/a_star/8_puzzle/a_star.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (puzzle matrix) calculateCost() int {
139139
h := 0
140140
for i := range puzzle {
141141
for j := range puzzle[i] {
142-
if puzzle[i][j] != solvedPuzzle[i][j] {
142+
if puzzle[i][j] != outputPuzzle[i][j] {
143143
h++
144144
}
145145
}

0 commit comments

Comments
 (0)