问题出在 `judge` 函数中,你在递归调用时没有正确地更新 `count` 变量。在递归调用 `judge` 函数时,你应该将返回的计数值累加到当前的 `count` 变量中。以下是修改后的代码:
def not_in_control(lst):
return (lst[0] > lst[2] + 2 or lst[0] < lst[2]-2) and (lst[1] > lst[3] + 2 or lst[1] < lst[3]-2)
def route(str):
now=[0,0,str[2],str[3]]#now[0] is x,now[1] is y
count=0
return judge(now,count)
def judge(now,count):
if((now[0]+1==now[2] and now[1]==now[3])or(now[0]==now[2] and now[1]+1==now[3])):
count+=1
elif(now[0]+1<=now[2] and now[1]+1<=now[3] and not_in_control([now[0]+1,now[1],now[2],now[3]]) and not_in_control([now[0],now[1]+1,now[2],now[3]])):
count = judge([now[0]+1,now[1],now[2],now[3]],count)
count = judge([now[0],now[1]+1,now[2],now[3]],count)
elif(now[0]+1<=now[2] and not_in_control([now[0]+1,now[1],now[2],now[3]])):
count = judge([now[0]+1,now[1],now[2],now[3]],count)
elif(now[1]+1<=now[3] and not_in_control([now[0],now[1]+1,now[2],now[3]])):
count = judge([now[0],now[1]+1,now[2],now[3]],count)
else:
pass
return count
s=input()
str=s.split(' ')
if(len(str)>4):
exit('no more than 4 invalids')
for i in range(0,4):
if(str[i].isalnum):
str[i]=int(str[i])
if(str[i]<0 or str[i]>20):
exit('OverflowError')
else:
exit('invalid input')
#str[0] is dest.x,str[1] is dest.y,str[2] is ma.x,str[3] is ma.y
if(not_in_control(str)):
print(route(str))
现在,当你输入 `6 6 3 3` 时,输出应该是 6。 |