| Bytes | Lang | Time | Link |
|---|---|---|---|
| 219 | Nim | 250330T203320Z | janAkali |
| 300 | Python3 | 250330T151544Z | Ajax1234 |
| 270 | Python 3 | 191108T104203Z | Jitse |
| 022 | 05AB1E legacy | 200306T161819Z | Grimmy |
| 244 | Ruby 1.9 | 110425T111531Z | Ventero |
| 368 | ANSI C | 110425T023737Z | Jonathan |
| 339 | Python | 110425T055833Z | Keith Ra |
| 421 | Python | 110424T223047Z | Champo |
Nim, 219 bytes
Basically, a recursive flood fill.
Outputs to stderr.
var
m=stdin.readall
l=1+m.find'\n'
c=0
template d:auto=(if c>3:quit m else:return)
proc f(i=0)=((if i<0 or i>m.high:d);case m[i]
of'\n':d
of' ':m[i]='*'
of'@':l=l
else:return
c+=1
f i+1;f i-1;f i+l;f i-l)
f m.find'@'
Example output for two inputs:
####
# #
@*#####
#*****#
# **
#######
###*###################
###*********#*********#
##**#########******#**#
#*************#####**#
############### #@##
Python3, 300 bytes
E=enumerate
def f(b):
d={(x,y):v for x,r in E(b)for y,v in E(r)}
q=[(*i,[i])for i in d if'@'==d[i]]
for x,y,p in q:
for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
if(N:=(x+X,y+Y))not in d and' '==d[(x,y)]:
for J,K in p[1:]:b[J][K]='*'
return b
if' '==d.get(N)and N not in p:q+=[(*N,p+[N])]
Python 3, 270 bytes
import sys
def q(g,*s):w=len(g[0])+1;k='@'.join(g)+w*'@';*p,x=s or[k.find('#')];return'@'>k[x]and{x}-{*p}and[p,min((q(g,*s,x+a)or k for a in(-1,1,-w,w)),key=len)]['*'>k[x]]
g=''.join(sys.stdin.read());s=q(g.split('\n'))
for i,c in enumerate(g):print(end=[c,'+'][i in s])
Port of my answer to Find the shortest route on an ASCII road.
Uses '#' for start, '*' for end, '@' for wall and ' ' for empty space. In this, the function q is a helper function which returns a one-dimensional array with the shortest path in the maze. Function f can be shortened 4 bytes by not assigning the variable s. This is incredibly inefficient and will likely time out, since it calls the path-finding function for each character in the maze.
05AB1E (legacy), 23 22 bytes
[¾#4Føí„ *„@*δJ`:¬'*¢½
Modern 05AB1E, 26 bytes
[¾#4F€SøíJ„ *„@*â2ô`:¬'*¢½
Ruby 1.9, 244 characters
l=*$<
i=l*""
e=[]
d=[1,-1,x=l[0].size,-x]
r=[f=9e9]*s=x*b=l.size;r[i=~/@/]=0
r.map{i.gsub(/ /){r[p=$`.size]=d.map{|u|p>-u&&r[u+p]||f}.min+1;e<<p if p%x%~-~-x*(p/-~x%~-b)<1}}
r[g=e.find{|i|r[i]<f}].times{i[g]=?*;g+=d.find{|l|r[g]>r[g+l]}}
puts i
Output for the two examples:
####
# #
@*#####
#* #
#******
#######
###*###################
###* # *** #
## *######### *****#* #
# ************#####* #
############### #@##
Edits:
- (247 -> 245) Inlined e and renamed it to g
- (245 -> 249) Fix a bug when the exit is directly above the entrance
- (249 -> 246) Inlining + simplifications
- (246 -> 244) Shorter way to iterate over every field
ANSI C (384 373 368 characters)
Here's my C attempt. Compiled and run on Mac OS X.
m[9999],*r=m,*s=m,c,R=0,*a,L;
P(){while(*s++)putchar(*(s-1));}
C(int*l){if((l-s+2)%R==0||(l-s)%R==0||l-s<R||l>r-R)*l=42,P(),exit(0);}
e(int*l){if(*l==32)C(l),*l=42,e(l-1),*l=32,*l=42,e(l-R),*l=32,*l=42,e(l+1),*l=32,*l=42,e(l+R),*l=32;}
main(){while(~(c=getchar()))*r++=c,R=!R&&c==10?r-s:R,L=c==64?r-s-1:L;L%R==0&&e(s+L+1);(L+2)%R==0&&e(s+L-1);L<R&&e(s+L+R);e(s+L-R);}
Sample output for a couple of tests:
####
# #
@*#####
#*****#
# *#
#####*#
###*###################
###* #******** #
##**#########** #* #
#*************#####* #
############### #@##
Limitations: Only works for mazes up to 1000 characters, but this can easily be increased. I just picked an arbitrary number rather than bother to malloc/remalloc.
Also, this is the most warning-laden code I've ever written. 19 warnings, though it looks like even more with the XCode code highlighting. :D
EDITS: Edited and tested to drop int from main, to use ~ instead of !=EOF and putchar instead of printf. Thanks for the comments!
Python, 339 chars
import sys
M=list(sys.stdin.read())
L=len(M)
R=range(L)
N=M.index('\n')+1
D=2*L*[9e9]
D[M.index('@')+N]=0
J=(1,-1,N,-N)
for x in R:
for i in[N+i for i in R if' '==M[i]]:D[i]=min(1+D[i+j]for j in J)
e=[i+N for i in R[:N]+R[::N]+R[N-2::N]+R[-N:]if 0<D[i+N]<9e9][0]
while D[e]:M[e-N]='*';e=[e+j for j in J if D[e+j]<D[e]][0]
print''.join(M)
Generates a shortest path through the maze.
Output for example mazes:
####
# #
@*#####
#* #
#******
#######
###*###################
###* # *** #
## *######### *****#* #
# ************#####* #
############### #@##
Python - 510 421 chars
m=[]
i=raw_input
l=i()
x=y=-1
while l:
if y<0:x+=1;y=l.find('@')
m.append(list(l));l=i()
s=(x,y)
t={}
q=[s]
v={s:1}
while 1:
try:(i,j),q=q[0],q[1:];c=m[i][j]
except:continue
if c==' 'and(i*j==0)|(i+1==len(m))|(j+1==len(m[0])):break
for d,D in(-1,0),(0,-1),(1,0),(0,1):
p=(i+d,j+D)
if not p in v and'#'!=c:v[p]=0;q.append(p);t[p]=(i,j)
while(i,j)!=s:m[i][j]='*';i,j=t[(i,j)]
print'\n'.join(''.join(l)for l in m)