2025-08-18 14:16:41 +08:00
|
|
|
enum Operation {
|
|
|
|
Add,
|
|
|
|
Subtract,
|
|
|
|
Multiply,
|
|
|
|
Divide,
|
|
|
|
}
|
|
|
|
enum Card {
|
|
|
|
Int(i32),
|
|
|
|
Op(Operation),
|
|
|
|
}
|
2025-07-10 14:13:01 +08:00
|
|
|
struct Solution;
|
|
|
|
impl Solution {
|
2025-08-18 14:16:41 +08:00
|
|
|
fn solve(cards: &Vec<Card>) -> bool {
|
|
|
|
let mut stack = Vec::new();
|
|
|
|
for t in cards.iter() {
|
|
|
|
if let Card::Op(op) = t {
|
|
|
|
if stack.len() < 2 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let b = stack.pop().unwrap() as f64;
|
|
|
|
let a = stack.pop().unwrap() as f64;
|
|
|
|
let result = match op {
|
|
|
|
Operation::Add => a + b,
|
|
|
|
Operation::Subtract => a - b,
|
|
|
|
Operation::Multiply => a * b,
|
|
|
|
Operation::Divide => {
|
|
|
|
if b == 0f64 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
a / b
|
|
|
|
}
|
|
|
|
};
|
|
|
|
stack.push(result);
|
|
|
|
} else if let Card::Int(num) = t {
|
|
|
|
stack.push(*num as f64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stack.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
(24f64 - stack[0]).abs() < 1e-5
|
|
|
|
}
|
|
|
|
fn dfs(cards: &Vec<i32>, card_objects: &mut Vec<Card>, visited: &mut Vec<bool>) -> bool {
|
|
|
|
if card_objects.len() == 7 {
|
|
|
|
return Self::solve(card_objects);
|
2025-08-17 22:56:39 +08:00
|
|
|
}
|
2025-08-18 14:16:41 +08:00
|
|
|
for i in 0..8 {
|
|
|
|
if i <= 3 {
|
|
|
|
if visited[i] {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
visited[i] = true;
|
|
|
|
card_objects.push(Card::Int(cards[i]));
|
|
|
|
if Self::dfs(cards, card_objects, visited) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
card_objects.pop();
|
|
|
|
visited[i] = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let op = match i {
|
|
|
|
4 => Operation::Add,
|
|
|
|
5 => Operation::Subtract,
|
|
|
|
6 => Operation::Multiply,
|
|
|
|
_ => Operation::Divide,
|
|
|
|
};
|
|
|
|
card_objects.push(Card::Op(op));
|
|
|
|
if Self::dfs(cards, card_objects, visited) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
card_objects.pop();
|
2025-08-01 10:39:53 +08:00
|
|
|
}
|
2025-08-17 22:56:39 +08:00
|
|
|
}
|
2025-08-18 14:16:41 +08:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn judge_point24(cards: Vec<i32>) -> bool {
|
|
|
|
let mut visited = vec![false; 4];
|
|
|
|
let mut card_objects = Vec::new();
|
|
|
|
for i in 0..4 {
|
|
|
|
visited[i] = true;
|
|
|
|
card_objects.push(Card::Int(cards[i]));
|
|
|
|
for j in 0..4 {
|
|
|
|
if visited[j] {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
visited[j] = true;
|
|
|
|
card_objects.push(Card::Int(cards[j]));
|
|
|
|
if Self::dfs(&cards, &mut card_objects, &mut visited) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
card_objects.pop();
|
|
|
|
visited[j] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
card_objects.pop();
|
|
|
|
visited[i] = false;
|
2025-07-30 10:59:42 +08:00
|
|
|
}
|
2025-08-18 14:16:41 +08:00
|
|
|
false
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2025-08-18 14:16:41 +08:00
|
|
|
let sl = Solution::judge_point24(vec![3,3,8,8]);
|
2025-07-29 11:20:14 +08:00
|
|
|
println!("{:?}", sl);
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|