|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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,请问是哪里出了问题? |
|