Tap here to switch tabs
Problem
Submissions

Problem: Matrix Cells in Distance Order

Try to solve the Matrix Cells in Distance Order problem.

Statement

You are given four integers rows, cols, rCenter, and cCenter, representing a matrix with dimensions rows × \times cols and a specified center cell at coordinate (rCenter, cCenter).

Return a list of the coordinates of all cells in the matrix, ordered by their Manhattan distance from (rCenter, cCenter) in nondecreasing order.

The Manhattan distance between two cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|.

Note: If multiple cells have the same distance from the center, they may appear in any order relative to each other.

Constraints:

  • 1≤1 \leq rows, cols ≤100\leq 100

  • 0≤0 \leq rCenter << rows

  • 0≤0 \leq cCenter << cols

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Matrix Cells in Distance Order

Try to solve the Matrix Cells in Distance Order problem.

Statement

You are given four integers rows, cols, rCenter, and cCenter, representing a matrix with dimensions rows × \times cols and a specified center cell at coordinate (rCenter, cCenter).

Return a list of the coordinates of all cells in the matrix, ordered by their Manhattan distance from (rCenter, cCenter) in nondecreasing order.

The Manhattan distance between two cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|.

Note: If multiple cells have the same distance from the center, they may appear in any order relative to each other.

Constraints:

  • 1≤1 \leq rows, cols ≤100\leq 100

  • 0≤0 \leq rCenter << rows

  • 0≤0 \leq cCenter << cols