14
1
The lost pawn problem
After the chess game ended, a surviving pawn was left behind enemy lines. let's help him find the shortest way back home.
The original problem describes a nXn "chess" board, and a function f: {1,..,n-1}X{1,..,n}X{-1,0,1} => R+
of weights. the goal is to find the best path from some square in the buttom line, to some other square in the top line, where the possible moves are: left-up,up,right-up, and you may not exit the board.
The problem is relatively easy to solve in O(n^2) using dynamic programming, but this is codegolf, and we don't care about useless stuff like running time complexity...
The Problem
input: a 3-dimentional array (or some other collection of your choice, received through stdin, or as a function argument), corresponding to a regular chess board, exactly in size: 7X8X3 (#linePasses X #rowSize X #movesPerPass) containing non-negative integers.
the moves cost from some position (i,j)
where i
is the row index, and j
is the column index, are:
a[i][j][0]
for the cost to travel up-left to square(i+1,j-1)
, or graphically:\
.a[i][j][1]
for the cost to travel up to square(i+1,j)
, or graphically:|
.a[i][j][2]
for the cost to travel up-right to square(i+1,j+1)
, or graphically:/
.
you may assume it will not contain a path that sums greater than MAX_INT
.
output: an 8X8 ascii output showing the best (shortest, i.e. minimum sum of weights) path (if there is more than 1 optimal result, you may show an arbitrary path of your choice). the path is drawn bottom to top, where in each line, the character corresponding to the pawn's position in the path, is the one it about to make. e.g. if the pawn is about to move up-left from column 3 (to column 2), the you should draw:
#?######
##\#####
where ?
should be replaced with the next move. final position needs to be drawn as X
.
Examples
input:
[
[[1,1,1],[1,1,1],[0,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,0,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,0],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,0],[1,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,1],[1,0,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,1],[1,0,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,1],[0,1,1],[1,1,1],[1,1,1],[1,1,1],[1,1,1]]
]
output:
##X#####
###\####
###|####
###|####
##/#####
#/######
#|######
##\#####
input:
[
[[41,27,38],[12,83,32],[50,53,35],[46,32,26],[55,89,82],[75,30,87],[2,11,64],[8,55,22]],
[[56,21,0],[83,25,38],[43,75,63],[56,60,77],[68,55,89],[99,48,67],[94,30,9],[62,62,58]],
[[23,18,40],[24,47,61],[96,45,72],[71,6,48],[75,63,98],[93,56,51],[23,31,30],[49,34,99]],
[[20,47,42],[62,79,72],[32,28,44],[68,61,55],[62,39,57],[4,17,49],[97,85,6],[91,18,12]],
[[51,50,11],[32,39,56],[12,82,23],[33,88,87],[60,55,22],[29,78,14],[70,11,42],[63,94,67]],
[[75,64,60],[27,79,86],[70,72,56],[55,45,32],[95,67,12],[87,93,98],[81,36,53],[38,22,93]],
[[31,80,50],[77,71,22],[59,46,86],[64,71,53],[41,19,95],[62,71,22],[92,80,41],[26,74,29]]
]
output:
######X#
#####/##
####/###
#####\##
#####|##
######\#
######|#
#######\
this is code-golf, so shortest code wins.
play fair. no loopholes...
EDIT:
Iv'e written an un-golfed straight forward solution in scala you can look at. There's also a site you can play with scala code on-line: scalakata (just copy&paste the gist to scalakata, and hit the play button)
Iv'e never heard of Q, but thanks for such a detailed response :) – gilad hoch – 2016-05-30T15:40:44.713