Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
123
Task/Solve-a-Hopido-puzzle/Go/solve-a-hopido-puzzle-1.go
Normal file
123
Task/Solve-a-Hopido-puzzle/Go/solve-a-hopido-puzzle-1.go
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var board = []string{
|
||||
".00.00.",
|
||||
"0000000",
|
||||
"0000000",
|
||||
".00000.",
|
||||
"..000..",
|
||||
"...0...",
|
||||
}
|
||||
|
||||
var moves = [][2]int{
|
||||
{-3, 0}, {0, 3}, {3, 0}, {0, -3},
|
||||
{2, 2}, {2, -2}, {-2, 2}, {-2, -2},
|
||||
}
|
||||
|
||||
var grid [][]int
|
||||
|
||||
var totalToFill = 0
|
||||
|
||||
func solve(r, c, count int) bool {
|
||||
if count > totalToFill {
|
||||
return true
|
||||
}
|
||||
nbrs := neighbors(r, c)
|
||||
if len(nbrs) == 0 && count != totalToFill {
|
||||
return false
|
||||
}
|
||||
sort.Slice(nbrs, func(i, j int) bool {
|
||||
return nbrs[i][2] < nbrs[j][2]
|
||||
})
|
||||
|
||||
for _, nb := range nbrs {
|
||||
r = nb[0]
|
||||
c = nb[1]
|
||||
grid[r][c] = count
|
||||
if solve(r, c, count+1) {
|
||||
return true
|
||||
}
|
||||
grid[r][c] = 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func neighbors(r, c int) (nbrs [][3]int) {
|
||||
for _, m := range moves {
|
||||
x := m[0]
|
||||
y := m[1]
|
||||
if grid[r+y][c+x] == 0 {
|
||||
num := countNeighbors(r+y, c+x) - 1
|
||||
nbrs = append(nbrs, [3]int{r + y, c + x, num})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func countNeighbors(r, c int) int {
|
||||
num := 0
|
||||
for _, m := range moves {
|
||||
if grid[r+m[1]][c+m[0]] == 0 {
|
||||
num++
|
||||
}
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
func printResult() {
|
||||
for _, row := range grid {
|
||||
for _, i := range row {
|
||||
if i == -1 {
|
||||
fmt.Print(" ")
|
||||
} else {
|
||||
fmt.Printf("%2d ", i)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
nRows := len(board) + 6
|
||||
nCols := len(board[0]) + 6
|
||||
grid = make([][]int, nRows)
|
||||
for r := 0; r < nRows; r++ {
|
||||
grid[r] = make([]int, nCols)
|
||||
for c := 0; c < nCols; c++ {
|
||||
grid[r][c] = -1
|
||||
}
|
||||
for c := 3; c < nCols-3; c++ {
|
||||
if r >= 3 && r < nRows-3 {
|
||||
if board[r-3][c-3] == '0' {
|
||||
grid[r][c] = 0
|
||||
totalToFill++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pos, r, c := -1, 0, 0
|
||||
for {
|
||||
for {
|
||||
pos++
|
||||
r = pos / nCols
|
||||
c = pos % nCols
|
||||
if grid[r][c] != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
grid[r][c] = 1
|
||||
if solve(r, c, 2) {
|
||||
break
|
||||
}
|
||||
grid[r][c] = 0
|
||||
if pos >= nRows*nCols {
|
||||
break
|
||||
}
|
||||
}
|
||||
printResult()
|
||||
}
|
||||
98
Task/Solve-a-Hopido-puzzle/Go/solve-a-hopido-puzzle-2.go
Normal file
98
Task/Solve-a-Hopido-puzzle/Go/solve-a-hopido-puzzle-2.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
global nCells, cMap, best
|
||||
record Pos(r,c)
|
||||
|
||||
procedure main(A)
|
||||
puzzle := showPuzzle("Input",readPuzzle())
|
||||
QMouse(puzzle,findStart(puzzle),&null,0)
|
||||
showPuzzle("Output", solvePuzzle(puzzle)) | write("No solution!")
|
||||
end
|
||||
|
||||
procedure readPuzzle()
|
||||
# Start with a reduced puzzle space
|
||||
p := [[-1],[-1]]
|
||||
nCells := maxCols := 0
|
||||
every line := !&input do {
|
||||
put(p,[: -1 | -1 | gencells(line) | -1 | -1 :])
|
||||
maxCols <:= *p[-1]
|
||||
}
|
||||
every put(p, [-1]|[-1])
|
||||
# Now normalize all rows to the same length
|
||||
every i := 1 to *p do p[i] := [: !p[i] | (|-1\(maxCols - *p[i])) :]
|
||||
return p
|
||||
end
|
||||
|
||||
procedure gencells(s)
|
||||
static WS, NWS
|
||||
initial {
|
||||
NWS := ~(WS := " \t")
|
||||
cMap := table() # Map to/from internal model
|
||||
cMap["#"] := -1; cMap["_"] := 0
|
||||
cMap[-1] := " "; cMap[0] := "_"
|
||||
}
|
||||
|
||||
s ? while not pos(0) do {
|
||||
w := (tab(many(WS))|"", tab(many(NWS))) | break
|
||||
w := numeric(\cMap[w]|w)
|
||||
if -1 ~= w then nCells +:= 1
|
||||
suspend w
|
||||
}
|
||||
end
|
||||
|
||||
procedure showPuzzle(label, p)
|
||||
write(label," with ",nCells," cells:")
|
||||
every r := !p do {
|
||||
every c := !r do writes(right((\cMap[c]|c),*nCells+1))
|
||||
write()
|
||||
}
|
||||
return p
|
||||
end
|
||||
|
||||
procedure findStart(p)
|
||||
if \p[r := !*p][c := !*p[r]] = 1 then return Pos(r,c)
|
||||
end
|
||||
|
||||
procedure solvePuzzle(puzzle)
|
||||
if path := \best then {
|
||||
repeat {
|
||||
loc := path.getLoc()
|
||||
puzzle[loc.r][loc.c] := path.getVal()
|
||||
path := \path.getParent() | break
|
||||
}
|
||||
return puzzle
|
||||
}
|
||||
end
|
||||
|
||||
class QMouse(puzzle, loc, parent, val)
|
||||
|
||||
method getVal(); return val; end
|
||||
method getLoc(); return loc; end
|
||||
method getParent(); return parent; end
|
||||
method atEnd(); return nCells = val; end
|
||||
|
||||
method visit(r,c)
|
||||
if /best & validPos(r,c) then return Pos(r,c)
|
||||
end
|
||||
|
||||
method validPos(r,c)
|
||||
v := val+1
|
||||
xv := (0 <= puzzle[r][c]) | fail
|
||||
if xv = (v|0) then { # make sure this path hasn't already gone there
|
||||
ancestor := self
|
||||
while xl := (ancestor := \ancestor.getParent()).getLoc() do
|
||||
if (xl.r = r) & (xl.c = c) then fail
|
||||
return
|
||||
}
|
||||
end
|
||||
|
||||
initially
|
||||
val := val+1
|
||||
if atEnd() then return best := self
|
||||
QMouse(puzzle, visit(loc.r-3,loc.c), self, val)
|
||||
QMouse(puzzle, visit(loc.r-2,loc.c-2), self, val)
|
||||
QMouse(puzzle, visit(loc.r, loc.c-3), self, val)
|
||||
QMouse(puzzle, visit(loc.r+2,loc.c-2), self, val)
|
||||
QMouse(puzzle, visit(loc.r+3,loc.c), self, val)
|
||||
QMouse(puzzle, visit(loc.r+2,loc.c+2), self, val)
|
||||
QMouse(puzzle, visit(loc.r, loc.c+3), self, val)
|
||||
QMouse(puzzle, visit(loc.r-2,loc.c+2), self, val)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue