From 6213d83856cfbbd3afd9510c865d43f1e9c89ce8 Mon Sep 17 00:00:00 2001 From: li_chx Date: Tue, 28 Apr 2026 09:33:19 +0800 Subject: [PATCH] =?UTF-8?q?2033.=20=E8=8E=B7=E5=8F=96=E5=8D=95=E5=80=BC?= =?UTF-8?q?=E7=BD=91=E6=A0=BC=E7=9A=84=E6=9C=80=E5=B0=8F=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 146 ++++++++++------------------------------------------ 1 file changed, 26 insertions(+), 120 deletions(-) diff --git a/src/main.rs b/src/main.rs index 456c60d..711476d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,131 +4,37 @@ mod arr; struct Solution {} impl Solution { - fn check( - grid: &Vec>, - mut x: i32, - mut y: i32, - mut last_x: i32, - mut last_y: i32, - ) -> bool { - if x < 0 || y < 0 || x >= grid[0].len() as i32 || y >= grid.len() as i32 { - return false; - } - // u r d l 1 2 4 8 - let interfaces = vec![0,10, 5, 12, 6, 9, 3]; - loop { - match grid[y as usize][x as usize] { - 1 => { - if x > last_x { - last_x = x; - last_y = y; - x += 1; - } else { - last_x = x; - last_y = y; - x -= 1; - }; - } - 2 => { - if y > last_y { - last_x = x; - last_y = y; - y += 1; - } else { - last_x = x; - last_y = y; - y -= 1; - }; - } - 3 => { - if x > last_x { - last_y = y; - last_x = x; - y += 1; - } else { - last_x = x; - last_y = y; - x -= 1; - }; - } - 4 => { - if x < last_x { - last_x = x; - last_y = y; - y += 1; - } else { - last_x = x; - last_y = y; - x += 1; - }; - } - 5 => { - if x > last_x { - last_x = x; - last_y = y; - y -= 1; - } else { - last_x = x; - last_y = y; - x -= 1; - } - } - 6 => { - if x < last_x { - last_x = x; - last_y = y; - y -= 1; - } else { - last_x = x; - last_y = y; - x += 1; - } - } - _ => {} - } - if x < 0 - || y < 0 - || x >= grid[0].len() as i32 - || y >= grid.len() as i32 - || (x == 0 && y == 0) - || |grid: &Vec>| -> bool { - for t in vec![1, 2, 4, 8] { - if (interfaces[grid[y as usize][x as usize] as usize] & t != 0) - && (interfaces[grid[last_y as usize][last_x as usize] as usize] - & (if t < 3 { t << 2 } else { t >> 2 }) - != 0) - { - if (t == 1 && y > last_y )||(t == 2 && x < last_x )||(t == 4 && y last_x) { - return false; - } - } - } - true - }(&grid) - { - return false; - } - if x == grid[0].len() as i32 - 1 && y == grid.len() as i32 - 1 { - return true; - } - } - } - pub fn has_valid_path(grid: Vec>) -> bool { + pub fn min_operations(grid: Vec>, x: i32) -> i32 { if grid.len() == 1 && grid[0].len() == 1 { - return true; + return 0; } - match grid[0][0] { - 1 => Self::check(&grid, 1, 0, 0, 0), - 2 => Self::check(&grid, 0, 1, 0, 0), - 3 => Self::check(&grid, 0, 1, 0, 0), - 4 => Self::check(&grid, 0, 1, 0, 0) || Self::check(&grid, 1, 0, 0, 0), - 5 => false, - 6 => Self::check(&grid, 1, 0, 0, 0), - _ => false, + let mut arr = vec![0; grid[0].len() * grid.len()]; + for i in 0..grid.len() { + for j in 0..grid[i].len() { + arr[i * grid[0].len() + j] = grid[i][j]; + } } + arr.sort_unstable(); + let a = arr[arr.len() / 2]; + let check = |grid: &Vec>, a: i32| { + let mut ans = 0; + for i in 0..grid.len() { + for j in 0..grid[i].len() { + if grid[i][j].abs_diff(a) as i32 % x != 0 { + return -1; + } + ans += grid[i][j].abs_diff(a) as i32 / x; + } + } + ans + }; + check(&grid, a) } } fn main() { - println!("{:?}", Solution::has_valid_path(make_matrix("[[2,6,3],[6,5,2]]"))); + println!( + "{:?}", + Solution::min_operations(make_matrix("[[1,1,100]]"), 1) + ); }