zoukankan      html  css  js  c++  java
  • 剑指offer python版 机器人的运动范围

    class Solution:
        def judge(self, threshold, i, j):
            # sum(map(int, str(i) + str(j)))这一句简直精髓! 直接得到坐标位置的 位和! i,j是超过1位的数也可以完美解决!
            if sum(map(int, str(i) + str(j))) <= threshold:
                return True
            else:
                return False
     
        def findgrid(self, threshold, rows, cols, matrix, i, j):
            count = 0
            if i<rows and j<cols and i>=0 and j>=0 and self.judge(threshold, i, j) and matrix[i][j] == 0: # matrix[i][j]==0表示没走过这一格
                matrix[i][j] = 1  # 表示已经走过了
                count = 1 + self.findgrid(threshold, rows, cols, matrix, i, j+1) 
                + self.findgrid(threshold, rows, cols, matrix, i, j-1) 
                + self.findgrid(threshold, rows, cols, matrix, i+1, j) 
                + self.findgrid(threshold, rows, cols, matrix, i-1, j)
            return count
     
        def movingCount(self, threshold, rows, cols):
            matrix = [[0 for i in range(cols)] for j in range(rows)]
            count = self.findgrid(threshold, rows, cols, matrix, 0, 0)
            print(matrix)
            return count
     
    # test
    s = Solution()
    count = s.movingCount(9, 12, 12)
    print (count)
  • 相关阅读:
    同余 扩展欧几里得
    185. [USACO Oct08] 挖水井
    Dijkstra算法
    Floyed算法
    codves——1079 回家
    codves——1021 玛丽卡
    codves——5960 信使
    计算几何基础
    【正睿oi省选十连测】第一场
    [APIO2012]守卫
  • 原文地址:https://www.cnblogs.com/xzm123/p/9848596.html
Copyright © 2011-2022 走看看