一个M*N的梅花桩,求最少需要跳几次能到终点
例:
输入:
3 3
3 1 1
1 0 1
2 0 1
输出:
2
程序:
class Solution:
def __init__(self,m,n):#初始化梅花桩
self.m=m
self.n=n
self.bu=0
def isSafe(self,maze,x,y):#判断某个地方是不是能踩的
if x>=0 and x=0 and y0:
return True
return False
def findload(self,maze,x,y):#计算步数
if x==self.m-1 and y==self.n-1 and maze[x][y]>0:
#sol[x][y]=1
return 0
if self.isSafe(maze,x,y):
num=maze[x][y]
#sol[x][y] = 1
for i in range(num,0,-1):
down=self.findload(maze,x+i,y)
#self.bu+=1
#sol[x][y]=i
right=self.findload(maze,x,y+i)
#self.bu+=1
#sol[x][y]=i
if down<0 and right>=0:
return right+1
if down>=0 and right<0:
return down+1
if down>=0 and right>=0:
return min(down,right)+1
#sol[x][y]=0
return -100000
while True:
try:
mn=list(map(int,input().strip().split()))
m=mn[0]
n=mn[1]
A=Solution(m,n)
#sol = [[0 * n] for i in range(m)]
maze=[]
for i in range(m):
maze.append(list(map(int,input().strip().split())))
b=A.findload(maze,0,0)
for line in maze:
for i in line:
print(i,end=' ')
print('n')
def pr(x):
if x>=0:
print(x)
else:
print('无法到达')
pr(b)
except:
break



