2033. 获取单值网格的最小操作数
This commit is contained in:
parent
d0da066c58
commit
6213d83856
146
src/main.rs
146
src/main.rs
|
|
@ -4,131 +4,37 @@ mod arr;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn check(
|
pub fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32 {
|
||||||
grid: &Vec<Vec<i32>>,
|
|
||||||
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<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 {
|
if grid.len() == 1 && grid[0].len() == 1 {
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
match grid[0][0] {
|
let mut arr = vec![0; grid[0].len() * grid.len()];
|
||||||
1 => Self::check(&grid, 1, 0, 0, 0),
|
for i in 0..grid.len() {
|
||||||
2 => Self::check(&grid, 0, 1, 0, 0),
|
for j in 0..grid[i].len() {
|
||||||
3 => Self::check(&grid, 0, 1, 0, 0),
|
arr[i * grid[0].len() + j] = grid[i][j];
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
arr.sort_unstable();
|
||||||
|
let a = arr[arr.len() / 2];
|
||||||
|
let check = |grid: &Vec<Vec<i32>>, 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() {
|
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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue