题解 | #机器人的运动范围#
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param threshold int整型 # @param rows int整型 # @param cols int整型 # @return int整型 # class Solution: def movingCount(self , threshold: int, rows: int, cols: int) -> int: # write code here flag=[[0 for i in range(cols)] for j in range(rows)] for i in range(rows): for j in range(cols): sum1=0 for item in str(i): sum1+=int(item) for item in str(j): sum1+=int(item) if sum1<=threshold: flag[i][j]=1 for i in range(rows): for j in range(cols): if i==0 and j==0: flag[i][j]=1 elif i==0: if flag[i][j-1]==0: flag[i][j]=0 elif j==0: if flag[i-1][j]==0: flag[i][j]=0 else: if flag[i][j-1]==0 and flag[i-1][j]==0: flag[i][j]=0 res=0 for i in range(rows): for j in range(cols): res+=flag[i][j] return res