1391. 检查网格中是否存在有效路径
This commit is contained in:
parent
4460a2f028
commit
d0da066c58
136
src/main.rs
136
src/main.rs
|
|
@ -1,24 +1,134 @@
|
||||||
|
use crate::arr::make_matrix;
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn furthest_distance_from_origin(moves: String) -> i32 {
|
fn check(
|
||||||
let mut mv = 0;
|
grid: &Vec<Vec<i32>>,
|
||||||
let mut dif = 0;
|
mut x: i32,
|
||||||
for c in moves.chars() {
|
mut y: i32,
|
||||||
match c {
|
mut last_x: i32,
|
||||||
'L' => mv -= 1,
|
mut last_y: i32,
|
||||||
'R' => mv += 1,
|
) -> bool {
|
||||||
_ => dif += 1,
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mv >= 0 { mv + dif } else { -mv + dif }
|
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<Vec<i32>>| -> 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_y ) || (t == 8 && x > 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<Vec<i32>>) -> 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() {
|
fn main() {
|
||||||
println!(
|
println!("{:?}", Solution::has_valid_path(make_matrix("[[2,6,3],[6,5,2]]")));
|
||||||
"{:?}",
|
|
||||||
Solution::furthest_distance_from_origin("_R__LL_".into())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue