Given four integers row , cols , rCenter and cCenter . There is one rows x cols Matrix , Your coordinates on the cell are (rCenter, cCenter) .
Returns the coordinates of all cells in the matrix , And press and (rCenter, cCenter) From the smallest to the largest . You can return the answers in any order that meets this condition .
Cell (r1, c1) and (r2, c2) The distance between is |r1 - r2| + |c1 - c2|.
Input :rows = 1, cols = 2, rCenter = 0, cCenter = 0
Output :[[0,0],[0,1]]
explain : from (r0, c0) The distance to other cells is :[0,1]
Input :rows = 2, cols = 2, rCenter = 0, cCenter = 1
Output :[[0,1],[0,0],[1,1],[1,0]]
explain : from (r0, c0) The distance to other cells is :[0,1,1,2]
[[0,1],[1,1],[0,0],[1,0]] It will also be seen as the right answer .
Input :rows = 2, cols = 3, rCenter = 1, cCenter = 2
Output :[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
explain : from (r0, c0) The distance to other cells is :[0,1,1,2,2,3]
Other answers that meet the requirements of the questions will also be considered correct , for example [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
1 <= rows, cols <= 100
0 <= rCenter < rows
0 <= cCenter < cols
class Solution:
def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
ans = [[] for i in range(205)]
for i in range(rows):
for j in range(cols):
distance = abs(i - rCenter) + abs(j - cCenter)
ans[distance].append([i, j])
res = []
for i in ans:
if i:
res.extend(i)
return res