鱼C论坛

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

Python关于过河卒问题

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

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

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

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

棋盘用坐标表示,

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

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

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
    global count
    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]])):
        judge([now[0]+1,now[1],now[2],now[3]],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]])):
        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]])):
        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))

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

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-8 12:19:53 | 显示全部楼层
问题出在你的递归调用中。在你的 judge 函数中,当递归调用时,你应该传递 count 的值作为参数,而不是直接使用全局变量 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(lst):
    now = [0, 0, lst[2], lst[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)
    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))

这样修改后,你应该可以得到正确的输出了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-8 18:07:00 | 显示全部楼层
问题出在 `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。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

测试结果还是0啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-29 02:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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