Shortest path for a chess knight

12

3

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.

Keith Randall

Posted 2011-05-22T21:50:18.680

Reputation: 19 865

Answers

4

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(?.,"")

Ventero

Posted 2011-05-22T21:50:18.680

Reputation: 9 842

4

Haskell, 255 236 231 229 bytes

import Data.List
k x y=unlines[[toEnum$findIndices(elem(i,j))(scanl(\s _->filter(\(z,w)->z`elem`n&&w`elem`n)$(\(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
    let readArgs = map read args
    let out = k (readArgs !! 0) (readArgs !! 1)
    putStr out

Augmented Fourth

Posted 2011-05-22T21:50:18.680

Reputation: 71

2

Welcome to the site! Good first golf. If you want some additional ideas for Haskell golfing, check out https://codegolf.stackexchange.com/questions/19255/tips-for-golfing-in-haskell

– isaacg – 2017-05-13T06:25:37.587

map(\(c,d)->...)$zip ... can be shortened to zipWith(\c d->...).... – Laikoni – 2017-05-13T11:50:38.850

2

Windows PowerShell, 178 183 188

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.

Joey

Posted 2011-05-22T21:50:18.680

Reputation: 12 260

1

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][0],g+m[c][1]];a[d[0]]&&
a[d[0]][d[1]]&&a[d[0]][d[1]]>e&&(a[d[0]][d[1]]=e,b.push(d))}for(c=0;c<b.length;c++)
s(b[c][0],b[c][1],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.

pimvdb

Posted 2011-05-22T21:50:18.680

Reputation: 821

Take out all those vars and save yourself tons of space... – Ry- – 2011-05-26T22:36:57.193

@minitech: It's a recursive function so that messes everything up... Some vars could be removed though, thanks. – pimvdb – 2011-05-27T14:38:35.597