topic:
Topic links: https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
Problem-solving ideas:
The problem there is an implied condition that: to go from the start area must be 0,0 points in a row, if not continuous, the robot can not go, as shown below
The robot can not directly from the yellow zone, flew the red zone
It is not directly the sum of all digits of all the calculated area is smaller than k into the
Method a: BFS (time O (MN), a space O (MN))
0,0 points from the beginning of the search, if the current grid to meet the conditions, the current grid right and below the grid put into the queue to continue searching; if the condition is not met, then the right and below the current lattice grid can not reach, not Gag new child to the queue, the flow shown below:
variable:
- queue: a queue to be searched, initialization only a starting point (0,0)
- rec: Eligible searched queue, initially empty
Process:
First, the (0,0) point to join the queue to be searched:
Sum of digits 0 and less than k = 2, the (0,0) was added to the rec, continue to search for (0,0) and the right upper side of the lattice points 0,0:
queue in two cells (1,0) and (0,1) are the sum of the digits 1, in line with the conditions, added to the rec, the right and continue to top the search:
Method two: DP (time O (MN), a space O (N))
(0,0) starts traversing each row, traversal rules as follows:
- If the current is greater than the number of bits and k, then continue
- If the left and bottom edges of the grid are unreachable, then continue
- Other cases, updates can be reached grid plus one
Which, dp array does not need to create an array m × n, because in fact, we only need to know the circumstances of the current case and lower lattice grid lattice can be left
It creates only a 1 * n array, to satisfy the condition, the flow as shown below:
From the start line by line traversing point 0,0
Continue to traverse the second row, this time is stored in the array dp state of the first row, the second row when the traverse, determines the position corresponding to the value dp array, is equivalent to a state where the first row is determined
When it is judged the second row in the second cell, the state of each element of the array dp expressed as follows:
- dp [0] = 1 2 cell line
- dp [1] = 1 2 cell line
- dp [2] = 1 row of cells 3
- ...
Therefore, when the second cell row 2 is determined, it is determined only dp [0] and dp [1], is equivalent to determining the positions of the two necessary:
According to this logic, until complete traversal
Code:
method one:
import queue
class Solution:
def movingCount(self, m: int, n: int, k: int) -> int:
rec = queue.Queue()
rec.put((0,0))
res = set()
while not rec.empty():
x, y = rec.get()
tmp = sum([int(a) for a in str(x)]) + sum([int(a) for a in str(y)])
if tmp > k:
continue
elif x >= m or y >= n:
continue
elif (x, y) in res:
continue
res.add((x, y))
rec.put((x + 1, y))
rec.put((x, y + 1))
return len(res)
Method Two:
class Solution:
def movingCount(self, m: int, n: int, k: int) -> int:
# 多创建一个位置,可以避免每次都进行越界检查
rec = [0] * (n + 1)
rec[1] = 1
num = 0
for x in range(m):
# 如果上一行全部无法到达,本行也一定都无法到达,退出以提高效率
if 0 == sum(rec):
break
for y in range(1, n + 1):
tmp = sum([int(a) for a in str(x)]) + sum([int(a) for a in str(y - 1)])
if tmp > k:
rec[y] = 0
continue
# 当前右值中的rec[y],就是下面一行是否可到达的值
rec[y] = max(rec[y], rec[y - 1])
if 1 == rec[y]:
num += 1
return num