|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 磊之茶 于 2020-7-18 22:09 编辑
代码:def sc(mb=None):
if mb==None:
return None
c=mb.split('\n')
ln=0
relist={}
for i in c:
col=0
for a in i:
relist[(ln,col)]=True if a ==' ' else False
col+=1
ln+=1
return relist
def panduan(mb={},lnd=[(0,0)],worn=[],win):
if type(mb) != dict or (0,0) in worn:
return "I don't know!"
else:
ln,col=lnd[len(lnd)-1]
if lnd[len(lnd)-1]==win:
return lnd
d=(ln+1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln-1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col+1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col-1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
else:
lnd.pop()
worn.append(d)
panduan(mb,lnd,worn,win)
if __name__=='__main__':
d=input('请在下方输入这个迷宫的样子:(空格为没有)\n')
mu=sc(d)
for i in mu:
g=i
d=panduan(mu,win=g)
if d=="I don't know!":
print('你这是无解的迷宫!')
else:
for i in d:
mu[d]=''
for i in mu:
print(mu[i],end='')
它说缩进有问题。但它到底哪里有缩进问题了?
本帖最后由 wuqramy 于 2020-7-18 22:16 编辑
来看这一段:
def panduan(mb={},lnd=[(0,0)],worn=[],win):
if type(mb) != dict or (0,0) in worn:
return "I don't know!"
else:
ln,col=lnd[len(lnd)-1]
if lnd[len(lnd)-1]==win:
return lnd
d=(ln+1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln-1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col+1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col-1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
else:
lnd.pop()
worn.append(d)
panduan(mb,lnd,worn,win)
问题1:黄色部分影响了elif的运行,分别要加一个Tab
问题2:这里它报错:
SyntaxError: non-default argument follows default argument
翻译:SyntaxError:非默认参数跟随默认参数
我们要把非默认参数(win)放在默认参数前面定义
正确代码,试一试: def sc(mb=None):
if mb==None:
return None
c=mb.split('\n')
ln=0
relist={}
for i in c:
col=0
for a in i:
relist[(ln,col)]=True if a ==' ' else False
col+=1
ln+=1
return relist
def panduan(win,mb={},lnd=[(0,0)],worn=[]):
if type(mb) != dict or (0,0) in worn:
return "I don't know!"
else:
ln,col=lnd[len(lnd)-1]
if lnd[len(lnd)-1]==win:
return lnd
d=(ln+1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln-1,col)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col+1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
d=(ln,col-1)
elif d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb,lnd,worn,win)
else:
lnd.pop()
worn.append(d)
panduan(mb,lnd,worn,win)
if __name__=='__main__':
d=input('请在下方输入这个迷宫的样子:(空格为没有)\n')
mu=sc(d)
for i in mu:
g=i
d=panduan(mu,win=g)
if d=="I don't know!":
print('你这是无解的迷宫!')
else:
for i in d:
mu[d]=''
for i in mu:
print(mu[i],end='')
如果没有实现目的, elif这个错误用if分开写: def sc(mb=None):
if mb == None:
return None
c = mb.split('\n')
ln = 0
relist = {}
for i in c:
col = 0
for a in i:
relist[(ln, col)] = True if a == ' ' else False
col += 1
ln += 1
return relist
def panduan(mb={}, lnd=[(0, 0)], worn=[], win):
if type(mb) != dict or (0, 0) in worn:
return "I don't know!"
else:
ln, col = lnd[len(lnd) - 1]
if lnd[len(lnd) - 1] == win:
return lnd
d = (ln + 1, col)
if d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb, lnd, worn, win)
d = (ln - 1, col)
if d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb, lnd, worn, win)
d = (ln, col + 1)
if d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb, lnd, worn, win)
d = (ln, col - 1)
if d not in lnd and d not in worn and mb[d]:
lnd.append(d)
panduan(mb, lnd, worn, win)
else:
lnd.pop()
worn.append(d)
panduan(mb, lnd, worn, win)
if __name__ == '__main__':
d = input('请在下方输入这个迷宫的样子:(空格为没有)\n')
mu = sc(d)
for i in mu:
g = i
d = panduan(mu, win=g)
if d == "I don't know!":
print('你这是无解的迷宫!')
else:
for i in d:
mu[d] = ''
for i in mu:
print(mu[i], end='')
|
|