# 2022-10-25：在一个 2 * 3 的板上（board）有 5 块砖瓦，用数字 1~5 来表示

2022-10-25：在一个 2 * 3 的板上（board）有 5 块砖瓦，用数字 1~5 来表示,

``````use std::collections::HashSet;
impl Solution {
pub fn sliding_puzzle(m: Vec<Vec<i32>>) -> i32 {
unsafe {
nexts = vec![0, 0, 0];
let mut set: HashSet<i32> = HashSet::new();
let from =
m[0][0] * b6 + m[0][1] * b5 + m[0][2] * b4 + m[1][0] * b3 + m[1][1] * b2 + m[1][2];
let mut heap = Vec::new();
// [
// 从from出发到达当前状态，已经走了几步，
// 从当前状态到最终状态的估计距离
// 当前状态是什么，用数字代表
// ]
heap.push(vec![0, Solution::distance(from), from]);
let mut ans = -1;
while heap.len() > 0 {
heap.sort_by(|a, b| (b[0] + b[1]).cmp(&(a[0] + a[1])));
let arr = heap.pop().unwrap();
let distance = arr[0];
let cur = arr[2];
if set.contains(&cur) {
continue;
}
if cur == 123450 {
ans = distance;
break;
}
set.insert(cur);
let next_size = Solution::nexts(cur);
for i in 0..next_size {
let next = nexts[i as usize];
if !set.contains(&next) {
heap.push(vec![distance + 1, Solution::distance(next), next]);
}
}
}
return ans;
}
}

unsafe fn nexts(from: i32) -> i32 {
// 301245
//  10000
// a = 3
let a = from / b6;
// b = 0
let b = (from / b5) % 10;
// c = 1
let c = (from / b4) % 10;
// d = 2
let d = (from / b3) % 10;
// e = 4
let e = (from / b2) % 10;
// f = 5
let f = from % 10;
if a == 0 {
nexts[0] = from + (b - a) * b6 + (a - b) * b5;
nexts[1] = from + (d - a) * b6 + (a - d) * b3;
return 2;
} else if b == 0 {
nexts[0] = from + (a - b) * b5 + (b - a) * b6;
nexts[1] = from + (c - b) * b5 + (b - c) * b4;
nexts[2] = from + (e - b) * b5 + (b - e) * b2;
return 3;
} else if c == 0 {
nexts[0] = from + (b - c) * b4 + (c - b) * b5;
nexts[1] = from + (f - c) * b4 + (c - f);
return 2;
} else if d == 0 {
nexts[0] = from + (a - d) * b3 + (d - a) * b6;
nexts[1] = from + (e - d) * b3 + (d - e) * b2;
return 2;
} else if e == 0 {
nexts[0] = from + (b - e) * b2 + (e - b) * b5;
nexts[1] = from + (d - e) * b2 + (e - d) * b3;
nexts[2] = from + (f - e) * b2 + (e - f);
return 3;
} else {
nexts[0] = from + (e - f) + (f - e) * b2;
nexts[1] = from + (c - f) + (f - c) * b4;
return 2;
}
}

// 当前的数，num
// 最终要去的数，123450
// 返回num -> 123450 曼哈顿距离！
fn distance(num: i32) -> i32 {
let a = -1;
let b = i32::abs(a);
let mut ans = end[(num / b6) as usize][0] + end[(num / b6) as usize][1];
ans +=
end[((num / b5) % 10) as usize][0] + i32::abs(end[((num / b5) % 10) as usize][1] - 1);
ans +=
end[((num / b4) % 10) as usize][0] + i32::abs(end[((num / b4) % 10) as usize][1] - 2);
ans +=
i32::abs(end[((num / b3) % 10) as usize][0] - 1) + end[((num / b3) % 10) as usize][1];
ans += i32::abs(end[((num / b2) % 10) as usize][0] - 1)
+ i32::abs(end[((num / b2) % 10) as usize][1] - 1);
ans +=
i32::abs(end[(num % 10) as usize][0] - 1) + i32::abs(end[(num % 10) as usize][1] - 2);
return ans;
}
}

const b6: i32 = 100000;

const b5: i32 = 10000;

const b4: i32 = 1000;

const b3: i32 = 100;

const b2: i32 = 10;

static mut nexts: Vec<i32> = vec![];

const end: [[i32; 2]; 6] = [[1, 2], [0, 0], [0, 1], [0, 2], [1, 0], [1, 1]];

fn main() {
let nums = vec![vec![1, 2, 3], vec![4, 0, 5]];
let ans = Solution::sliding_puzzle(nums);
println!("ans = {:?}", ans);
}

struct Solution {}
``````

(=￣ω￣=)··· 暂无内容！

172

10

16

5