## 1030. Matrix Cells in Distance Order

We are given a matrix with `R`

rows and `C`

columns has cells with integer coordinates `(r, c)`

, where `0 <= r < R`

and `0 <= c < C`

.

Additionally, we are given a cell in that matrix with coordinates `(r0, c0)`

.

Return the coordinates of all cells in the matrix, sorted by their distance from `(r0, c0)`

from smallest distance to largest distance. Here, the distance between two cells `(r1, c1)`

and `(r2, c2)`

is the Manhattan distance, `|r1 - r2| + |c1 - c2|`

. (You may return the answer in any order that satisfies this condition.)

**Example 1:**

Input:R = 1, C = 2, r0 = 0, c0 = 0Output:[[0,0],[0,1]]Explanation:The distances from (r0, c0) to other cells are: [0,1]

**Example 2:**

Input:R = 2, C = 2, r0 = 0, c0 = 1Output:[[0,1],[0,0],[1,1],[1,0]]Explanation:The distances from (r0, c0) to other cells are: [0,1,1,2] The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

**Example 3:**

Input:R = 2, C = 3, r0 = 1, c0 = 2Output:[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]Explanation:The distances from (r0, c0) to other cells are: [0,1,1,2,2,3] There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

**Note:**

`1 <= R <= 100`

`1 <= C <= 100`

`0 <= r0 < R`

`0 <= c0 < C`

## Rust Solution

```
struct Solution;
impl Solution {
fn all_cells_dist_order(r: i32, c: i32, r0: i32, c0: i32) -> Vec<Vec<i32>> {
let mut cells: Vec<Vec<i32>> = vec![];
for i in 0..r {
for j in 0..c {
cells.push(vec![i, j]);
}
}
cells.sort_unstable_by_key(|v| (v[0] - r0).abs() + (v[1] - c0).abs());
cells
}
}
#[test]
fn test() {
let r = 1;
let c = 2;
let r0 = 0;
let c0 = 0;
let res: Vec<Vec<i32>> = vec_vec_i32![[0, 0], [0, 1]];
assert_eq!(Solution::all_cells_dist_order(r, c, r0, c0), res);
let r = 2;
let c = 2;
let r0 = 0;
let c0 = 1;
let res: Vec<Vec<i32>> = vec_vec_i32![[0, 1], [0, 0], [1, 1], [1, 0]];
assert_eq!(Solution::all_cells_dist_order(r, c, r0, c0), res);
let r = 2;
let c = 3;
let r0 = 1;
let c0 = 2;
let res: Vec<Vec<i32>> = vec_vec_i32![[1, 2], [0, 2], [1, 1], [0, 1], [1, 0], [0, 0]];
assert_eq!(Solution::all_cells_dist_order(r, c, r0, c0), res);
}
```

Having problems with this solution? Click here to submit an issue on github.