LeetcodeMedium- [Interview Question 13. The range of motion of the robot]

There is a grid with m rows and n columns on the ground, from coordinate [0,0] to coordinate [m-1, n-1]. A robot starts from the grid of coordinates [0, 0], it can move one grid to the left, right, up, and down at a time (it cannot move outside the grid), nor can it enter the row and column coordinates. k's lattice. For example, when k is 18, the robot can enter the square [35, 37] because 3 + 5 + 3 + 7 = 18. But it cannot enter the square [35, 38] because 3 + 5 + 3 + 8 = 19. How many grids can the robot reach?

 

Example 1:

Input: m = 2, n = 3, k = 1
Output: 3


Example 2:

Input: m = 3, n = 1, k = 0
Output: 1


prompt:

1 <= n,m <= 100
0 <= k <= 20

Source: LeetCode
Link: https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof
Copyright belongs to the deduction network Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

table of Contents

Idea 1: Deep search (dfs)

Idea 2: Guangsuo (bfs)

Idea 3: Recursion


Idea 1: Deep search (dfs)

Starting from the starting point (0, 0), searching in four directions, the condition that can continue is that the number of coordinates is not greater than K.

The traditional deep search is in four directions, but this question can be optimized as: right and down.

class Solution:
    def movingCount(self, m: int, n: int, k: int) -> int:
        # 暴力
        self.cnt = 0
        self.n = n
        self.m = m
        self.k = k
        self.visit = [[0]*n for _ in range(m)]
        self.dx = [0, 1] # 优化为只:向右、向下
        self.dy = [1, 0]
        
        self.visit[0][0] = 1
        self.dfs(0, 0)

        return self.cnt
    
    def dfs(self, x, y):
        ti = x
        tj = y
        t = 0
        while ti > 0:
            t += ti % 10
            ti = ti // 10
        while tj > 0:
            t += tj % 10
            tj = tj // 10
        if t <= self.k:
            self.cnt += 1
        else:
            return 
        for i in range(2):
            xx = x + self.dx[i]
            yy = y + self.dy[i]
            if 0 <= xx < self.m and 0 <= yy < self.n and self.visit[xx][yy] == 0:
                self.visit[xx][yy] = 1
                self.dfs(xx, yy)
        

Idea 2: Guangsuo (bfs)

Guangsou is implemented using a queue, and this question uses set to deduplicate.

def digitsum(n):
    ans = 0
    while n:
        ans += n % 10
        n //= 10
    return ans

class Solution:
    def movingCount(self, m: int, n: int, k: int) -> int:
        from queue import Queue
        q = Queue()
        q.put((0, 0))
        s = set()
        while not q.empty():
            x, y = q.get()
            if (x, y) not in s and 0 <= x < m and 0 <= y < n and digitsum(x) + digitsum(y) <= k:
                s.add((x, y))
                q.put((x, y + 1))
                q.put((x + 1, y))
        return len(s)

    

Idea 3: Recursion

def digitsum(n):
    ans = 0
    while n:
        ans += n % 10
        n //= 10
    return ans

class Solution:
    def movingCount(self, m: int, n: int, k: int) -> int:
        s = set([(0, 0)])
        for i in range(m):
            for j in range(n):
                if ((i-1,j) in s or (i,j-1) in s) and digitsum(i) + digitsum(j) <= k:
                    s.add((i, j))
        return len(s)

 

Published 314 original articles · 22 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_39451578/article/details/105427334