-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximize_grid_happiness.rs
111 lines (107 loc) · 3.16 KB
/
maximize_grid_happiness.rs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 最大化网格幸福感
// https://leetcode.cn/problems/maximize-grid-happiness
// INLINE ../../images/math/maximize_grid_happiness.jpeg
use std::cmp;
pub struct Solution;
impl Solution {
pub fn get_max_grid_happiness(
m: i32,
n: i32,
introverts_count: i32,
extroverts_count: i32,
) -> i32 {
let mut dp = vec![vec![vec![vec![-1; 250]; 7]; 7]; 26];
let pow1 = 3i32.pow(n as u32);
let pow2 = 3i32.pow((n - 1) as u32);
let offset = vec![vec![0, 0, 0], vec![0, -60, -10], vec![0, -10, 40]];
fn dfs(
cur: i32,
a: i32,
b: i32,
status: i32,
dp: &mut Vec<Vec<Vec<Vec<i32>>>>,
m: i32,
n: i32,
pow1: i32,
pow2: i32,
offset: &Vec<Vec<i32>>,
) -> i32 {
if cur == m * n {
return 0;
}
if dp[cur as usize][a as usize][b as usize][status as usize] != -1 {
return dp[cur as usize][a as usize][b as usize][status as usize];
}
// let x = cur / n;
let y = cur % n;
let first_state = status / pow2;
let last_state = status % 3;
let next_status = status * 3 % pow1;
let mut ans = dfs(cur + 1, a, b, next_status, dp, m, n, pow1, pow2, offset);
#[allow(unused_assignments)]
let mut diff = 0;
if a > 0 {
diff = 120
+ offset[1][first_state as usize]
+ (if y > 0 {
offset[1][last_state as usize]
} else {
0
});
ans = cmp::max(
ans,
diff + dfs(
cur + 1,
a - 1,
b,
next_status + 1,
dp,
m,
n,
pow1,
pow2,
offset,
),
);
}
if b > 0 {
diff = 40
+ offset[2][first_state as usize]
+ (if y > 0 {
offset[2][last_state as usize]
} else {
0
});
ans = cmp::max(
ans,
diff + dfs(
cur + 1,
a,
b - 1,
next_status + 2,
dp,
m,
n,
pow1,
pow2,
offset,
),
);
}
dp[cur as usize][a as usize][b as usize][status as usize] = ans;
ans
}
dfs(
0,
introverts_count,
extroverts_count,
0,
&mut dp,
m,
n,
pow1,
pow2,
&offset,
)
}
}