Python关于过河卒问题
题目描述棋盘上
A 点有一个过河卒,需要走到目标 B 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 C 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。
棋盘用坐标表示,
A 点(0,0)、B 点(n,m),同样马的位置坐标是需要给出的。
现在要求你计算出卒从 A点能够到达 B点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。
def not_in_control(lst):
return (lst > lst + 2 or lst < lst-2) and (lst > lst + 2 or lst < lst-2)
def route(str):
now=,str]#now is x,now is y
global count
count=0
return judge(now,count)
def judge(now,count):
if((now+1==now and now==now)or(now==now and now+1==now)):
count+=1
elif(now+1<=now and now+1<=now and not_in_control(+1,now,now,now]) and not_in_control(,now+1,now,now])):
judge(+1,now,now,now],count)
judge(,now+1,now,now],count)
elif(now+1<=now and not_in_control(+1,now,now,now])):
judge(+1,now,now,now],count)
elif(now+1<=now and not_in_control(,now+1,now,now])):
judge(,now+1,now,now],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.isalnum):
str=int(str)
if(str<0 or str>20):
exit('OverflowError')
else:
exit('invalid input')
#str is dest.x,str is dest.y,str is ma.x,str is ma.y
if(not_in_control(str)):
print(route(str))
我首先判断马的控制区域,然后定义judge函数来实现递归计数,
输入 6 6 3 3,预计输出应该为6,但测试结果为0,请问是哪里出了问题? 问题出在你的递归调用中。在你的 judge 函数中,当递归调用时,你应该传递 count 的值作为参数,而不是直接使用全局变量 count 。因为在每次递归调用时,你都需要传递当前的计数值,而不是使用全局变量。
以下是修改后的代码:
def not_in_control(lst):
return (lst > lst + 2 or lst < lst-2) and (lst > lst + 2 or lst < lst-2)
def route(lst):
now = , lst]# now is x, now is y
count = 0
return judge(now, count)
def judge(now, count):
if (now + 1 == now and now == now) or (now == now and now + 1 == now):
count += 1
elif now + 1 <= now and now + 1 <= now and not_in_control( + 1, now, now, now]) and not_in_control(, now + 1, now, now]):
count = judge( + 1, now, now, now], count)
count = judge(, now + 1, now, now], count)
elif now + 1 <= now and not_in_control( + 1, now, now, now]):
count = judge( + 1, now, now, now], count)
elif now + 1 <= now and not_in_control(, now + 1, now, now]):
count = judge(, now + 1, now, now], count)
return count
s = input()
lst = s.split(' ')
if len(lst) > 4:
exit('no more than 4 invalids')
for i in range(0, 4):
if not lst[ i].isdigit():
exit('invalid input')
lst[ i] = int(lst[ i])
if lst[ i] < 0 or lst[ i] > 20:
exit('OverflowError')
if not_in_control(lst):
print(route(lst))
这样修改后,你应该可以得到正确的输出了。 问题出在 `judge` 函数中,你在递归调用时没有正确地更新 `count` 变量。在递归调用 `judge` 函数时,你应该将返回的计数值累加到当前的 `count` 变量中。以下是修改后的代码:
def not_in_control(lst):
return (lst > lst + 2 or lst < lst-2) and (lst > lst + 2 or lst < lst-2)
def route(str):
now=,str]#now is x,now is y
count=0
return judge(now,count)
def judge(now,count):
if((now+1==now and now==now)or(now==now and now+1==now)):
count+=1
elif(now+1<=now and now+1<=now and not_in_control(+1,now,now,now]) and not_in_control(,now+1,now,now])):
count = judge(+1,now,now,now],count)
count = judge(,now+1,now,now],count)
elif(now+1<=now and not_in_control(+1,now,now,now])):
count = judge(+1,now,now,now],count)
elif(now+1<=now and not_in_control(,now+1,now,now])):
count = judge(,now+1,now,now],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.isalnum):
str=int(str)
if(str<0 or str>20):
exit('OverflowError')
else:
exit('invalid input')
#str is dest.x,str is dest.y,str is ma.x,str is ma.y
if(not_in_control(str)):
print(route(str))
现在,当你输入 `6 6 3 3` 时,输出应该是 6。 Mike_python小 发表于 2023-10-8 18:07
问题出在 `judge` 函数中,你在递归调用时没有正确地更新 `count` 变量。在递归调用 `judge` 函数时,你应 ...
测试结果还是0啊{:9_241:}
页:
[1]