Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
102
Task/Solve-a-Numbrix-puzzle/Phix/solve-a-numbrix-puzzle.phix
Normal file
102
Task/Solve-a-Numbrix-puzzle/Phix/solve-a-numbrix-puzzle.phix
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
sequence board, knownx, knowny
|
||||
|
||||
integer size, limit, nchars, tries
|
||||
string fmt, blank
|
||||
|
||||
constant ROW = 1, COL = 2
|
||||
constant moves = {{-1,0},{0,-1},{0,1},{1,0}}
|
||||
|
||||
function onboard(integer row, integer col)
|
||||
return row>=1 and row<=size and col>=nchars and col<=nchars*size
|
||||
end function
|
||||
|
||||
function solve(integer row, integer col, integer n)
|
||||
integer nrow, ncol
|
||||
tries+= 1
|
||||
if n>limit then return 1 end if
|
||||
if knownx[n] then
|
||||
for move=1 to length(moves) do
|
||||
nrow = row+moves[move][ROW]
|
||||
ncol = col+moves[move][COL]*nchars
|
||||
if nrow = knownx[n]
|
||||
and ncol = knowny[n] then
|
||||
if solve(nrow,ncol,n+1) then return 1 end if
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
return 0
|
||||
end if
|
||||
sequence wmoves = {}
|
||||
for move=1 to length(moves) do
|
||||
nrow = row+moves[move][ROW]
|
||||
ncol = col+moves[move][COL]*nchars
|
||||
if onboard(nrow,ncol)
|
||||
and board[nrow][ncol]='.' then
|
||||
board[nrow][ncol-nchars+1..ncol] = sprintf(fmt,n)
|
||||
if solve(nrow,ncol,n+1) then return 1 end if
|
||||
board[nrow][ncol-nchars+1..ncol] = blank
|
||||
end if
|
||||
end for
|
||||
return 0
|
||||
end function
|
||||
|
||||
procedure Numbrix(sequence s)
|
||||
integer y, ch, ch2, k
|
||||
atom t0 = time()
|
||||
s = split(s,'\n')
|
||||
size = length(s)
|
||||
limit = size*size
|
||||
nchars = length(sprintf(" %d",limit))
|
||||
fmt = sprintf(" %%%dd",nchars-1)
|
||||
blank = repeat('.',nchars)
|
||||
board = repeat(repeat(' ',size*nchars),size)
|
||||
knownx = repeat(0,limit)
|
||||
knowny = repeat(0,limit)
|
||||
for x=1 to size do
|
||||
for y=nchars to size*nchars by nchars do
|
||||
ch = s[x][y]
|
||||
if ch!='.' then
|
||||
k = ch-'0'
|
||||
ch2 = s[x][y-1]
|
||||
if ch2!=' ' then
|
||||
k += (ch2-'0')*10
|
||||
board[x][y-1] = ch2
|
||||
end if
|
||||
knownx[k] = x
|
||||
knowny[k] = y
|
||||
end if
|
||||
board[x][y] = ch
|
||||
end for
|
||||
end for
|
||||
tries = 0
|
||||
if solve(knownx[1],knowny[1],2) then
|
||||
puts(1,join(board,"\n"))
|
||||
printf(1,"\nsolution found in %d tries (%3.2fs)\n",{tries,time()-t0})
|
||||
else
|
||||
puts(1,"no solutions found\n")
|
||||
end if
|
||||
end procedure
|
||||
|
||||
constant board1 = """
|
||||
. . . . . . . . .
|
||||
. . 46 45 . 55 74 . .
|
||||
. 38 . . 43 . . 78 .
|
||||
. 35 . . . . . 71 .
|
||||
. . 33 . . . 59 . .
|
||||
. 17 . . . . . 67 .
|
||||
. 18 . . 11 . . 64 .
|
||||
. . 24 21 . 1 2 . .
|
||||
. . . . . . . . ."""
|
||||
Numbrix(board1)
|
||||
|
||||
constant board2 = """
|
||||
. . . . . . . . .
|
||||
. 11 12 15 18 21 62 61 .
|
||||
. 6 . . . . . 60 .
|
||||
. 33 . . . . . 57 .
|
||||
. 32 . . . . . 56 .
|
||||
. 37 . 1 . . . 73 .
|
||||
. 38 . . . . . 72 .
|
||||
. 43 44 47 48 51 76 77 .
|
||||
. . . . . . . . ."""
|
||||
Numbrix(board2)
|
||||
Loading…
Add table
Add a link
Reference in a new issue