题目要求:
思路:
思路1:递归遍历(伪代码)
movingCount(threshold, rows, cols) :
根据入参定义map地图,位置(x,y)处值为1,标识可以走,0标识已经走过
调用移动moving函数,传参(第几行,第几列,threshold,rows,cols,Map),由题意从0,0位置开始
返回地图上0的数量,即为移动最大格子数
moving(当前位置第几行,当前位置第几列,threshold,rows行,cols列,Map地图):
先将当前位置标记为走过,即该位置值从1修改为0
如果在该点能向左走
如果该点没被走过并且符合要求,就向左递归
如果在该点能向右走
如果该点没被走过并且符合要求,就向右递归
如果在该点能向上走
该点没被走过并且符合要求,就向上递归
如果在该点能向下走
该点没被走过并且符合要求,就向下递归
##### 代码如下:
思路1:
```python
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param threshold int整型
# @param rows int整型
# @param cols int整型
# @return int整型
#
class Solution:
def movingCount(self , threshold, rows, cols):
Map = [[1 for i in range(cols)] for j in range(rows)]
self.moving(0,0,threshold,rows,cols,Map)
return str(Map).count('0')
# write code here
def moving(self,x,y,threshold,rows,cols,Map):
Map[x][y] = 0
if x < rows-1:
if Map[x+1][y] and sum([int(item) for item in list((str(x+1)+str(y)))]) <= threshold:
self.moving(x+1,y,threshold,rows,cols,Map)
if 0 < x:
if Map[x-1][y] and sum([int(item) for item in list((str(x-1)+str(y)))]) <= threshold:
self.moving(x-1,y,threshold,rows,cols,Map)
if y < cols-1:
if Map[x][y+1] and sum([int(item) for item in list((str(x)+str(y+1)))]) <= threshold:
self.moving(x,y+1,threshold,rows,cols,Map)
if 0 < y:
if Map[x][y-1] and sum([int(item) for item in list((str(x)+str(y-1)))]) <= threshold:
self.moving(x,y-1,threshold,rows,cols,Map)