54. 螺旋矩阵
This commit is contained in:
parent
44119f4286
commit
f29016175d
146
src/main.rs
146
src/main.rs
|
@ -1,106 +1,62 @@
|
||||||
enum Operation {
|
|
||||||
Add,
|
|
||||||
Subtract,
|
|
||||||
Multiply,
|
|
||||||
Divide,
|
|
||||||
}
|
|
||||||
enum Card {
|
|
||||||
Int(i32),
|
|
||||||
Op(Operation),
|
|
||||||
}
|
|
||||||
struct Solution;
|
struct Solution;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn solve(cards: &Vec<Card>) -> bool {
|
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
|
||||||
let mut stack = Vec::new();
|
let mut top_x = 0;
|
||||||
for t in cards.iter() {
|
let mut left_y = 0;
|
||||||
if let Card::Op(op) = t {
|
let mut bottom_x = matrix.len() as i32 - 1;
|
||||||
if stack.len() < 2 {
|
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;
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
pos = new_pos;
|
||||||
if stack.len() != 1 {
|
res.push(matrix[pos.0 as usize][pos.1 as usize]);
|
||||||
return false;
|
true
|
||||||
}
|
};
|
||||||
(24f64 - stack[0]).abs() < 1e-5
|
while next_pos() {}
|
||||||
}
|
res
|
||||||
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);
|
|
||||||
}
|
|
||||||
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<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;
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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);
|
println!("{:?}", sl);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue