# Shortest path for a chess knight

Your program should compute the number of moves it takes a chess knight to reach each square of the chess board. The input will be two space-separated integers representing coordinates of the starting square (horizontal then vertical coordinate, 0-7 inclusive each). Your program should output a number grid containing the minimum number of moves a chess knight needs to make to reach each square.

examples

input

0 0


output

03232345
34123434
21432345
32323434
23234345
34343454
43434545
54545456


input

3 1


output

21232123
32303232
21232123
34121432
23232323
32323234
43434343
34343434


Shortest code wins.

## Ruby 1.9, 146 151 characters

g=(?9*8+".
")*8
r=->x,a=0{x<0||a<g[x].to_i&&(g[x]=a.to_s;[21,19,12,8].map{|i|r[x+i,a+1];r[x-i,a+1]})}
r[eval gets.split*?++"*10"]
puts g.tr(?.,"")


import Data.List
k x y=unlines[[toEnum$findIndices(elem(i,j))(scanl(\s _->filter(\(z,w)->zelemn&&welemn)$(\(a,b)->[(a+c,b+d)|(c,d)<-zip[1,1,-1,-1,-2,-2,2,2][2,-2,2,-2,1,-1,1,-1]])=<<s)[(x,y)]n)!!0+48|j<-n]|i<-n]where n=[0..7]


D:

This is my first attempt at golfing. Also somewhat new to Haskell.

Test suite:

import System.Environment

main :: IO ()
main = do
args <- getArgs
putStr out

• Welcome to the site! Good first golf. If you want some additional ideas for Haskell golfing, check out codegolf.stackexchange.com/questions/19255/… – isaacg May 13 '17 at 6:25
• map(\(c,d)->...)$zip ... can be shortened to zipWith(\c d->...).... – Laikoni May 13 '17 at 11:50 ## Windows PowerShell, 178 183188 filter f($n){if($d[($p=$_)]-gt$n){$d[$p]=$n 12,8,21,19|%{$p+$_$p-$_}|f($n+1)}}$d=,0*20+(0..7|%{,9*8+0,0})+,0*20$x,$y=-split$input
20+"$y$x"|f 0
2..9|%{-join$d[(10*$_).."\$_7"]}


Passes both test cases.

## JavaScript, 426 408 bytes

for(a=[],i=0;i<8;i++){a[i]=[];for(j=0;j<8;j++)a[i][j]=99}m=[[2,1],[2,-1],
[-2,1],[-2,-1],[1,2],[-1,2],[1,-2],[-1,-2]];function s(f,g,e,b){b&&(a[f][g]=0);
for(var b=[],c=0;c<m.length;c++){var d=[f+m[c],g+m[c]];a[d]&&
a[d][d]&&a[d][d]>e&&(a[d][d]=e,b.push(d))}for(c=0;c<b.length;c++)
s(b[c],b[c],e+1)}function _(f,g){s(g,f,1,1);for(e="",b=0;b<8;b++)e+=
a[b].join("")+"\n";return e}


JavaScript is not the most concise language out there... But my coding style is a little verbose too.

Usage: _(0, 0) etc.

• Take out all those vars and save yourself tons of space... – Ry- May 26 '11 at 22:36
• @minitech: It's a recursive function so that messes everything up... Some var`s could be removed though, thanks. – pimvdb May 27 '11 at 14:38