From f29016175d88ffefd9c7a60195e3fb336467f736 Mon Sep 17 00:00:00 2001 From: li-chx Date: Thu, 4 Sep 2025 11:31:56 +0800 Subject: [PATCH] =?UTF-8?q?54.=20=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 146 ++++++++++++++++++---------------------------------- 1 file changed, 51 insertions(+), 95 deletions(-) diff --git a/src/main.rs b/src/main.rs index 45d338b..08c0e4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,106 +1,62 @@ -enum Operation { - Add, - Subtract, - Multiply, - Divide, -} -enum Card { - Int(i32), - Op(Operation), -} struct Solution; impl Solution { - fn solve(cards: &Vec) -> bool { - let mut stack = Vec::new(); - for t in cards.iter() { - if let Card::Op(op) = t { - if stack.len() < 2 { + pub fn spiral_order(matrix: Vec>) -> Vec { + let mut top_x = 0; + let mut left_y = 0; + let mut bottom_x = matrix.len() as i32 - 1; + let mut right_y = matrix[0].len() as i32 - 1; + let mut next_x = 0; + let mut next_y = 1; + let mut pos = (0, 0); + + let mut res = vec![matrix[0][0]]; + + let mut next_pos = || { + let mut new_pos = (pos.0 + next_x, pos.1 + next_y); + if new_pos.0 > bottom_x || new_pos.0 < top_x || new_pos.1 > right_y || new_pos.1 < left_y { + (next_x, next_y) = match (next_x, next_y) { + (0, 1) => { + top_x += 1; + (1, 0) + } + (1, 0) => { + right_y -= 1; + (0, -1) + } + (0, -1) => { + bottom_x -= 1; + (-1, 0) + } + (-1, 0) => { + left_y += 1; + (0, 1) + } + _ => (0, 0), + }; + + new_pos = (pos.0 + next_x, pos.1 + next_y); + if new_pos.0 > bottom_x + || new_pos.0 < top_x + || new_pos.1 > right_y + || new_pos.1 < left_y + { 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, card_objects: &mut Vec, visited: &mut Vec) -> bool { - if card_objects.len() == 7 { - return Self::solve(card_objects); - } - 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(); - } - } - false - } - - pub fn judge_point24(cards: Vec) -> 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; - } - false + pos = new_pos; + res.push(matrix[pos.0 as usize][pos.1 as usize]); + true + }; + while next_pos() {} + res } } fn main() { - let sl = Solution::judge_point24(vec![3,3,8,8]); + let sl = Solution::spiral_order(vec![ + vec![1, 2, 3, 4], + vec![5, 6, 7, 8], + vec![9, 10, 11, 12], + ]); println!("{:?}", sl); }