-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 773
- Loading branch information
Showing
6 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,61 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func isOk773(board [2][3]int) bool { | ||
return board[0][0] == 1 && board[0][1] == 2 && board[0][2] == 3 && | ||
board[1][0] == 4 && board[1][1] == 5 | ||
} | ||
|
||
type qItem773 struct { | ||
key [2][3]int | ||
zeroIndex [2]int | ||
} | ||
|
||
var dir773 = [4][2]int{ | ||
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, | ||
} | ||
|
||
func Solution(board [][]int) int { | ||
|
||
key := [2][3]int{ | ||
{board[0][0], board[0][1], board[0][2]}, | ||
{board[1][0], board[1][1], board[1][2]}, | ||
} | ||
zeroIndex := [2]int{0, 0} | ||
for i := range 2 { | ||
for j := range 3 { | ||
if board[i][j] == 0 { | ||
zeroIndex = [2]int{i, j} | ||
} | ||
} | ||
} | ||
queue := []qItem773{ | ||
{key, zeroIndex}, | ||
} | ||
used := map[[2][3]int]struct{}{ | ||
key: struct{}{}, | ||
} | ||
steps := 0 | ||
for len(queue) > 0 { | ||
nq := make([]qItem773, 0) | ||
for _, cur := range queue { | ||
if isOk773(cur.key) { | ||
return steps | ||
} | ||
x, y := cur.zeroIndex[0], cur.zeroIndex[1] | ||
for _, d := range dir773 { | ||
nx, ny := x+d[0], y+d[1] | ||
if nx >= 0 && nx <= 1 && ny >= 0 && ny <= 2 { | ||
b := cur.key | ||
b[nx][ny], b[x][y] = b[x][y], b[nx][ny] | ||
if _, ok := used[b]; !ok { | ||
nq = append(nq, qItem773{b, [2]int{nx, ny}}) | ||
used[b] = struct{}{} | ||
} | ||
} | ||
} | ||
} | ||
queue = nq | ||
steps++ | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters