Groovy、624バイト。フォア!
時間は大きなボールでボールを転がします。argとして複数行の文字列を取りますQ
Q={a->d=[0]*4
a.eachWithIndex{x,y->f=x.indexOf('S');e=x.indexOf('E');
if(f!=-1){d[0]=f;d[1]=y}
if(e!=-1){d[2]=e;d[3]=y}}
g=[]
s={x,y,h,i,j->if(h.contains([x, y])|y>=a.size()||x>=a[y].size()|x<0|y<0)return;k = a[y][x]
def l=h+[[x, y]]
def m=j
def n=1
if(h){
o=h[-1]
p=[x,y]
q=[p[0]-o[0],p[1]-o[1]]
n=[[-2,0]:0,[-1,-1]:1,[1,-1]:2,[2,0]:3,[1,1]:4,[-1,1]:5][q]
r=n-i
m=j+((r==-5|r==5)?' LR'[(int)r/5]:['','R','RR','LL','L'][r])+'F'}
if(k=='E')g+=m
if(k=='+'|k=='S'){s(x-2,y,l,n,m)
s(x+2,y,l,n,m)
s(x+1,y+1,l,n,m)
s(x+1,y-1,l,n,m)
s(x-1,y+1,l,n,m)
s(x-1,y-1,l,n,m)}}
s(d[0],d[1],[],1,'')
print(g.min{it.size()}?:"Invalid maze!")}
ゴルフされていないバージョン:
def map =
"""
+ 0 0 0 0 0 0
0 0 0 0 0 + + 0
0 0 E 0 + 0 0 + 0
0 0 0 0 0 0 0 +
0 + 0 0 + + +
0 0 + + 0 0
S + 0 0 0""".split('\n').findAll()
//map =
// """
// 0 + +
//E + 0 S 0
// 0 0 0 +
// + + +""".split('\n').findAll()
//map = [""]// TODO remove this, this is type checking only
//map.remove(0)
//reader = System.in.newReader()
//line = reader.readLine()
//while (line != '') {
// map << line
// line = reader.readLine()
//}
startAndEnd = [0, 0, 0, 0]
map.eachWithIndex { it, idx ->
s = it.indexOf('S'); e = it.indexOf('E');
if (s != -1) {
startAndEnd[0] = s; startAndEnd[1] = idx
}
if (e != -1) {
startAndEnd[2] = e; startAndEnd[3] = idx
}
}
def validPaths = []
testMove = { x, y, visited ->// visited is an array of x y pairs that we have already visited in this tree
if (visited.contains([x, y]) || y >= map.size() || x >= map[y].size() || x < 0 || y < 0)
return;
def valueAtPos = map[y][x]
def newPath = visited + [[x, y]]
if (valueAtPos == 'E') validPaths += [newPath]
if (valueAtPos == '+' || valueAtPos == 'S') {
println "$x, $y passed $valueAtPos"
testMove(x - 2, y, newPath)
testMove(x + 2, y, newPath)
testMove(x + 1, y + 1, newPath)
testMove(x + 1, y - 1, newPath)
testMove(x - 1, y + 1, newPath)
testMove(x - 1, y - 1, newPath)
}
}
//if (!validPath) invalid()
testMove(startAndEnd[0], startAndEnd[1], [])
println validPaths.join('\n')
//println validPath
def smallest = validPaths.collect {
def path = ''
def orintation = 1
it.inject { old, goal ->
def chr = map[goal[1]][goal[0]]
def sub = [goal[0] - old[0], goal[1] - old[1]]
def newOrin = [[-2, 0]: 0, [-1, -1]: 1, [1, -1]: 2, [2, 0]: 3, [1, 1]:4, [-1, 1]:5][sub]
def diff = newOrin - orintation// 5L -5R
def addedPath= ((diff==-5||diff==5)?' LR'[(int)diff/5]:['', 'R', 'RR', 'LL', 'L'][diff]) + 'F'//(diff == 0) ? '' : (diff > 0 ? 'R'*diff : 'L'*(-diff)) + 'F'
// println "old:$old, goal:$goal chr $chr, orintation $orintation, sub:$sub newOrin $newOrin newPath $addedPath diff $diff"
path += addedPath
orintation = newOrin
goal
}
path
}.min{it.size()}
//println "paths:\n${smallest.join('\n')}"
if (smallest)
println "path $smallest"
else
println "Invalid maze!"