剑指offer 66. 机器人的运动范围

描述
地上有一个rows行和cols列的方格。坐标从 [0,0] 到 [rows-1,cols-1]。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于threshold的格子。 例如,当threshold为18时,机器人能够进入方格[35,37],因为3+5+3+7 = 18。但是,它不能进入方格[35,38],因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

范围:
1 <= rows, cols<= 100
0 <= threshold <= 20

示例1
输入:
1,2,3
返回值:
3

示例2
输入:
0,1,3
返回值:
1

示例3
输入:
10,1,100
返回值:
29
说明:
[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28] 这29种,后面的[0,29],[0,30]以及[0,31]等等是无法到达的

第一种,递归深度优先

# -*- coding:utf-8 -*-
class Solution:
    
    def __init__(self):
        self.visit = [[0 for j in range(100)] for i in range(100)]
    
    def getSum(self, v):
        res=0
        while v>0:
            res += (v%10)
            v=v/10
        return res
    
    def move(self, threshold, rows, cols, row, col, visited):
        if row<0 or row>=rows or col<0 or col>=cols:
            return 0
        if self.getSum(row) + self.getSum(col) > threshold:
            return 0
        if visited[row][col]==1:
            return 0
        visited[row][col]=1
        self.visit[row][col]=1
        downward = self.move(threshold, rows, cols, row+1, col, visited)
        upward = self.move(threshold, rows, cols, row-1, col, visited)
        right = self.move(threshold, rows, cols, row, col+1, visited)
        left = self.move(threshold, rows, cols, row, col-1, visited)
#         visited[row][col]=0
        return downward + upward + right + left + 1
    
    def movingCount(self, threshold, rows, cols):
        # write code here
        if rows == 0 or cols ==0:
            return 0
        if threshold == 0:
            return 1
        res = 0
        for i in range(rows):
            for j in range(cols):
                if self.visit[i][j] == 0:
                    visited = [[0 for t1 in range(cols)] for t2 in range(rows)]
                    cnt = self.move(threshold, rows, cols, i, j, visited)
                    if res < cnt:
                        res = cnt
        return res
                    

第二种方式, 广度优先

# -*- coding:utf-8 -*-
class Solution:
    def isLessThanThreshold(self, threshold, r, c):
        res = 0
        while r>0:
            res += (r%10)
            r = r/10
        while c>0:
            res += (c%10)
            c = c/10
        if res <= threshold:
            return True
        return False
    
    def isValid(self, r, c, rows, cols, threshold):
        if r<0 or r>=rows or c<0 or c>=cols:
            return False
        return self.isLessThanThreshold(threshold, r, c)
    
    def movingCount(self, threshold, rows, cols):
        # write code here
        if rows == 0 or cols == 0:
            return 0
        if threshold == 0:
            return 1
        s = []
        s.append((0,0))
        visited = [[0 for j in range(cols)] for i in range(rows)]
        visited[0][0] = 1
        res = 0
        while len(s)>0:
            r, c = s.pop()
            res += 1
            if self.isValid(r+1, c, rows, cols, threshold) and visited[r+1][c]==0:
                visited[r+1][c]=1
                s.append((r+1, c))
            if self.isValid(r-1, c, rows, cols, threshold) and visited[r-1][c]==0:
                visited[r-1][c]=1
                s.append((r-1, c))
            if self.isValid(r, c+1, rows, cols, threshold) and visited[r][c+1]==0:
                visited[r][c+1]=1
                s.append((r, c+1))
            if self.isValid(r, c-1, rows, cols, threshold) and visited[r][c-1]==0:
                visited[r][c-1]=1
                s.append((r, c-1))
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值