鱼C论坛

 找回密码
 立即注册
查看: 3185|回复: 3

Python关于过河卒问题

[复制链接]
发表于 2023-10-8 12:18:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述
棋盘上
A 点有一个过河卒,需要走到目标 B 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 C 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。

棋盘用坐标表示,

A 点(0,0)、B 点(n,m),同样马的位置坐标是需要给出的。

现在要求你计算出卒从 A点能够到达 B点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。


  1. def not_in_control(lst):  
  2.     return (lst[0] > lst[2] + 2 or lst[0] < lst[2]-2) and (lst[1] > lst[3] + 2 or lst[1] < lst[3]-2)  
  3.             
  4. def route(str):
  5.     now=[0,0,str[2],str[3]]#now[0] is x,now[1] is y
  6.     global count
  7.     count=0
  8.     return judge(now,count)
  9.    
  10. def judge(now,count):
  11.     if((now[0]+1==now[2] and now[1]==now[3])or(now[0]==now[2] and now[1]+1==now[3])):
  12.         count+=1
  13.     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]])):
  14.         judge([now[0]+1,now[1],now[2],now[3]],count)
  15.         judge([now[0],now[1]+1,now[2],now[3]],count)
  16.     elif(now[0]+1<=now[2] and not_in_control([now[0]+1,now[1],now[2],now[3]])):
  17.         judge([now[0]+1,now[1],now[2],now[3]],count)
  18.     elif(now[1]+1<=now[3] and not_in_control([now[0],now[1]+1,now[2],now[3]])):
  19.         judge([now[0],now[1]+1,now[2],now[3]],count)
  20.     else:
  21.         pass
  22.     return count
  23.      
  24. s=input()
  25. str=s.split(' ')
  26. if(len(str)>4):
  27.     exit('no more than 4 invalids')
  28. for i in range(0,4):
  29.     if(str[i].isalnum):
  30.         str[i]=int(str[i])
  31.         if(str[i]<0 or str[i]>20):
  32.             exit('OverflowError')
  33.     else:
  34.         exit('invalid input')
  35. #str[0] is dest.x,str[1] is dest.y,str[2] is ma.x,str[3] is ma.y

  36. if(not_in_control(str)):
  37.     print(route(str))
复制代码


我首先判断马的控制区域,然后定义judge函数来实现递归计数,
输入 6 6 3 3,预计输出应该为6,但测试结果为0,请问是哪里出了问题?

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-8 12:19:53 | 显示全部楼层
问题出在你的递归调用中。在你的 judge 函数中,当递归调用时,你应该传递 count 的值作为参数,而不是直接使用全局变量 count 。因为在每次递归调用时,你都需要传递当前的计数值,而不是使用全局变量。

以下是修改后的代码:

  1. def not_in_control(lst):
  2.     return (lst[0] > lst[2] + 2 or lst[0] < lst[2]-2) and (lst[1] > lst[3] + 2 or lst[1] < lst[3]-2)

  3. def route(lst):
  4.     now = [0, 0, lst[2], lst[3]]  # now[0] is x, now[1] is y
  5.     count = 0
  6.     return judge(now, count)

  7. def judge(now, count):
  8.     if (now[0] + 1 == now[2] and now[1] == now[3]) or (now[0] == now[2] and now[1] + 1 == now[3]):
  9.         count += 1
  10.     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]]):
  11.         count = judge([now[0] + 1, now[1], now[2], now[3]], count)
  12.         count = judge([now[0], now[1] + 1, now[2], now[3]], count)
  13.     elif now[0] + 1 <= now[2] and not_in_control([now[0] + 1, now[1], now[2], now[3]]):
  14.         count = judge([now[0] + 1, now[1], now[2], now[3]], count)
  15.     elif now[1] + 1 <= now[3] and not_in_control([now[0], now[1] + 1, now[2], now[3]]):
  16.         count = judge([now[0], now[1] + 1, now[2], now[3]], count)
  17.     return count

  18. s = input()
  19. lst = s.split(' ')
  20. if len(lst) > 4:
  21.     exit('no more than 4 invalids')
  22. for i in range(0, 4):
  23.     if not lst[ i].isdigit():
  24.         exit('invalid input')
  25.     lst[ i] = int(lst[ i])
  26.     if lst[ i] < 0 or lst[ i] > 20:
  27.         exit('OverflowError')

  28. if not_in_control(lst):
  29.     print(route(lst))
复制代码


这样修改后,你应该可以得到正确的输出了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-8 18:07:00 | 显示全部楼层
问题出在 `judge` 函数中,你在递归调用时没有正确地更新 `count` 变量。在递归调用 `judge` 函数时,你应该将返回的计数值累加到当前的 `count` 变量中。以下是修改后的代码:


  1. def not_in_control(lst):  
  2.     return (lst[0] > lst[2] + 2 or lst[0] < lst[2]-2) and (lst[1] > lst[3] + 2 or lst[1] < lst[3]-2)  
  3.             
  4. def route(str):
  5.     now=[0,0,str[2],str[3]]#now[0] is x,now[1] is y
  6.     count=0
  7.     return judge(now,count)
  8.    
  9. def judge(now,count):
  10.     if((now[0]+1==now[2] and now[1]==now[3])or(now[0]==now[2] and now[1]+1==now[3])):
  11.         count+=1
  12.     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]])):
  13.         count = judge([now[0]+1,now[1],now[2],now[3]],count)
  14.         count = judge([now[0],now[1]+1,now[2],now[3]],count)
  15.     elif(now[0]+1<=now[2] and not_in_control([now[0]+1,now[1],now[2],now[3]])):
  16.         count = judge([now[0]+1,now[1],now[2],now[3]],count)
  17.     elif(now[1]+1<=now[3] and not_in_control([now[0],now[1]+1,now[2],now[3]])):
  18.         count = judge([now[0],now[1]+1,now[2],now[3]],count)
  19.     else:
  20.         pass
  21.     return count
  22.      
  23. s=input()
  24. str=s.split(' ')
  25. if(len(str)>4):
  26.     exit('no more than 4 invalids')
  27. for i in range(0,4):
  28.     if(str[i].isalnum):
  29.         str[i]=int(str[i])
  30.         if(str[i]<0 or str[i]>20):
  31.             exit('OverflowError')
  32.     else:
  33.         exit('invalid input')
  34. #str[0] is dest.x,str[1] is dest.y,str[2] is ma.x,str[3] is ma.y

  35. if(not_in_control(str)):
  36.     print(route(str))
复制代码



现在,当你输入 `6 6 3 3` 时,输出应该是 6。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-9 17:40:52 | 显示全部楼层
Mike_python小 发表于 2023-10-8 18:07
问题出在 `judge` 函数中,你在递归调用时没有正确地更新 `count` 变量。在递归调用 `judge` 函数时,你应 ...

测试结果还是0啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 20:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表