-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1219_path_with_maximum_gold.rs
More file actions
55 lines (48 loc) · 1.47 KB
/
s1219_path_with_maximum_gold.rs
File metadata and controls
55 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![allow(unused)]
pub struct Solution {}
use std::cmp::max;
const DIRECTIONS: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, -1), (0, 1)];
impl Solution {
pub fn get_maximum_gold(mut grid: Vec<Vec<i32>>) -> i32 {
let mut ret = 0;
for row in 0..grid.len() {
for col in 0..grid[0].len() {
if grid[row][col] > 0 {
ret = max(
ret,
Self::dfs(&mut grid, row as i32, col as i32) + grid[row][col],
)
}
}
}
return ret;
}
fn dfs(grid: &mut Vec<Vec<i32>>, row: i32, col: i32) -> i32 {
let orginal = grid[row as usize][col as usize];
grid[row as usize][col as usize] = 0;
let mut ret = 0;
for di in DIRECTIONS.iter() {
let (next_row, next_col) = (row + di.0, col + di.1);
if next_row >= 0
&& next_row < grid.len() as i32
&& next_col >= 0
&& next_col < grid[0].len() as i32
&& grid[next_row as usize][next_col as usize] != 0
{
ret = max(
ret,
grid[next_row as usize][next_col as usize]
+ Self::dfs(grid, next_row, next_col),
);
}
}
grid[row as usize][col as usize] = orginal;
ret
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1219() {}
}