그루비, 624 바이트 전부!
시간 큰 공으로 구르는 공을 얻으십시오. 여러 줄 문자열을 인수로 취합니다.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!"