Knight's tour: Difference between revisions

oops, wrong task
(oops, wrong task)
Line 2,239:
100 551 556 561 102 777 572 771 104 781 57...
557 554 101 552 571 562 103 776 573 770 10...</lang>
 
===Breadth First?===
 
Breadth first searches are a natural for J, though they can be inefficient for some problems.
 
For this case, let's represent the potential boards as characters instead of numbers (for compactness) and let's limit the memory used to 16GB (that should be more than enough), and let's count how many solutions we find:
 
<lang J>9!:21]2^34
 
unpack=:verb define
mask=. +./' '~:y
board=. (255 0 1{a.) {~ {.@:>:@:"."0 mask#"1 y
)
 
ex1=:unpack ];._2]0 :0
0 0 0
0 0 0
0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0 0 0
0 0 0
0 0 0
)
 
solve=:verb define
board=.,:y
for_move.1+i.+/({.a.)=,y do.
board=. ;move <@knight"2 board
end.
)
 
kmoves=: ,/(2 1,:1 2)*"1/_1^#:i.4
 
knight=:dyad define
pos=. ($y)#:(,y)i.x{a.
moves=. <"1(#~ 0&<: */"1@:* ($y)&>"1)pos+"1 kmoves
moves=. (#~ (0{a.)={&y) moves
moves y adverb def (':';'y x} m')"0 (x+1){a.
)</lang>
 
Letting that cook, we find:
 
<lang J> $sol=:solve ex1
48422 8 8</lang>
 
48422 solutions.
 
That's a bit much to display here.
 
=={{header|Java}}==
{{Works with|Java|7}}
6,962

edits