Knight's tour: Difference between revisions

Added 11l
(Added 11l)
Line 21:
* [[Solve the no connection puzzle]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>V _kmoves = [(2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1)]
 
F chess2index(=chess, boardsize)
‘Convert Algebraic chess notation to internal index format’
chess = chess.lowercase()
V x = chess[0].code - ‘a’.code
V y = boardsize - Int(chess[1..])
R (x, y)
 
F boardstring(board, boardsize)
V r = 0 .< boardsize
V lines = ‘’
L(y) r
lines ‘’= "\n"r.map(x -> (I @board[(x, @y)] {‘#2’.format(@board[(x, @y)])} E ‘ ’)).join(‘,’)
R lines
 
F knightmoves(board, P, boardsize)
V (Px, Py) = P
V kmoves = Set(:_kmoves.map((x, y) -> (@Px + x, @Py + y)))
kmoves = Set(Array(kmoves).filter((x, y) -> x C 0 .< @boardsize & y C 0 .< @boardsize & !@board[(x, y)]))
R kmoves
 
F accessibility(board, P, boardsize)
[(Int, (Int, Int))] access
V brd = copy(board)
L(pos) knightmoves(board, P, boardsize' boardsize)
brd[pos] = -1
access.append((knightmoves(brd, pos, boardsize' boardsize).len, pos))
brd[pos] = 0
R access
 
F knights_tour(start, boardsize, _debug = 0B)
[(Int, Int) = Int] board
L(x) 0 .< boardsize
L(y) 0 .< boardsize
board[(x, y)] = 0
V move = 1
V P = chess2index(start, boardsize)
board[P] = move
move++
I _debug
print(boardstring(board, boardsize' boardsize))
L move <= board.len
P = min(accessibility(board, P, boardsize))[1]
board[P] = move
move++
I _debug
print(boardstring(board, boardsize' boardsize))
input("\n#2 next: ".format(move))
R board
 
L(boardsize, start) [(5, ‘c3’), (8, ‘h8’), (10, ‘e6’)]
print(‘boardsize: ’boardsize)
print(‘Start position: ’start)
V board = knights_tour(start, boardsize)
print(boardstring(board, boardsize' boardsize))
print()</lang>
 
{{out}}
<pre>
boardsize: 5
Start position: c3
 
19,12,17, 6,21
2, 7,20,11,16
13,18, 1,22, 5
8, 3,24,15,10
25,14, 9, 4,23
 
boardsize: 8
Start position: h8
 
38,41,18, 3,22,27,16, 1
19, 4,39,42,17, 2,23,26
40,37,54,21,52,25,28,15
5,20,43,56,59,30,51,24
36,55,58,53,44,63,14,29
9, 6,45,62,57,60,31,50
46,35, 8,11,48,33,64,13
7,10,47,34,61,12,49,32
 
boardsize: 10
Start position: e6
 
29, 4,57,24,73, 6,95,10,75, 8
58,23,28, 5,94,25,74, 7,100,11
3,30,65,56,27,72,99,96, 9,76
22,59, 2,63,68,93,26,81,12,97
31,64,55,66, 1,82,71,98,77,80
54,21,60,69,62,67,92,79,88,13
49,32,53,46,83,70,87,42,91,78
20,35,48,61,52,45,84,89,14,41
33,50,37,18,47,86,39,16,43,90
36,19,34,51,38,17,44,85,40,15
 
</pre>
 
=={{header|360 Assembly}}==
1,481

edits