From d0da066c58dd7b67b6a221d7b0997d64a9b2e6d5 Mon Sep 17 00:00:00 2001 From: li_chx Date: Mon, 27 Apr 2026 09:39:39 +0800 Subject: [PATCH] =?UTF-8?q?1391.=20=E6=A3=80=E6=9F=A5=E7=BD=91=E6=A0=BC?= =?UTF-8?q?=E4=B8=AD=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8=E6=9C=89=E6=95=88?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1ef779c..456c60d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,134 @@ +use crate::arr::make_matrix; + mod arr; struct Solution {} impl Solution { - pub fn furthest_distance_from_origin(moves: String) -> i32 { - let mut mv = 0; - let mut dif = 0; - for c in moves.chars() { - match c { - 'L' => mv -= 1, - 'R' => mv += 1, - _ => dif += 1, + 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; } } - if mv >= 0 { mv + dif } else { -mv + dif } + } + pub fn has_valid_path(grid: Vec>) -> bool { + if grid.len() == 1 && grid[0].len() == 1 { + return true; + } + 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, + } } } fn main() { - println!( - "{:?}", - Solution::furthest_distance_from_origin("_R__LL_".into()) - ); + println!("{:?}", Solution::has_valid_path(make_matrix("[[2,6,3],[6,5,2]]"))); }