3311. 构造符合图结构的二维矩阵

This commit is contained in:
li-chx 2025-09-08 21:57:43 +08:00
parent 817cef54c7
commit f82b9e1b8c
2 changed files with 168 additions and 18 deletions

25
src/arr.rs Normal file
View File

@ -0,0 +1,25 @@
pub fn make_arr(s: &str) -> Vec<Option<i32>> {
s.trim_matches(&['[', ']'][..])
.split(',')
.map(|x| {
let x = x.trim();
if x == "null" {
None
} else {
Some(x.parse::<i32>().unwrap())
}
})
.collect()
}
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
s.trim_matches(&['[', ']'][..])
.split("],[")
.map(|row| {
row.trim_matches(&['[', ']'][..])
.split(',')
.map(|x| x.trim().parse::<i32>().unwrap())
.collect()
})
.collect()
}

View File

@ -1,28 +1,153 @@
mod arr;
struct Solution;
use std::collections::HashMap;
impl Solution {
pub fn pancake_sort(arr: Vec<i32>) -> Vec<i32> {
let mut arr = arr;
let mut ans = vec![];
for i in (1..arr.len()+1).rev() {
if arr[i-1] == i as i32 {
continue;
}
let mut index = 0;
while arr[index] != i as i32 {
index += 1;
}
ans.push((index + 1) as i32);
arr[0..index + 1].reverse();
ans.push(i as i32);
arr[0..i].reverse();
println!("{:?}", arr);
pub fn construct_grid_layout(n: i32, edges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
if n == 1 {
return vec![vec![0]];
}
let mut connections: HashMap<i32, Vec<i32>> = HashMap::new();
let mut degree: Vec<Vec<i32>> = vec![vec![]; 5];
for edge in &edges {
connections.entry(edge[0]).or_insert(vec![]).push(edge[1]);
connections.entry(edge[1]).or_insert(vec![]).push(edge[0]);
}
connections.iter().for_each(|(k, v)| {
degree[v.len()].push(*k);
});
if degree[1].len() != 0 {
let mut ans = vec![degree[1][0]];
let mut last = -1;
loop {
let next = connections[&ans[ans.len() - 1]]
.iter()
.find(|&&x| x != last)
.cloned();
if let Some(next) = next {
ans.push(next);
last = ans[ans.len() - 2];
} else {
break;
}
}
return vec![ans];
}
let d3 = degree[3].len() as f32;
let d4 = degree[4].len() as f32;
let delta = (d3 * d3) / 4.0 - 4.0 * d4;
if delta >= 0.0 {
let sqrt_delta = delta.sqrt();
let mut x = (d3 / 2.0 + sqrt_delta) / 2.0;
let mut y = (d3 / 2.0 - sqrt_delta) / 2.0;
if x > y {
std::mem::swap(&mut (x), &mut (y));
}
let x = x as usize + 2;
let y = y as usize + 2;
let mut ans = vec![vec![-1; x]; y];
ans[0][0] = degree[2][0];
for i in 0..y {
for j in 0..x {
if i == 0 && j == 0 {
if x == 2 {
if connections[&connections[&ans[0][0]][0]].len() == 2{
ans[0][1] = connections[&ans[0][0]][0];
ans[1][0] = connections[&ans[0][0]][1];
}
else {
ans[0][1] = connections[&ans[0][0]][1];
ans[1][0] = connections[&ans[0][0]][0];
}
continue;
}
let mut count_a = 1;
let mut cur = connections[&ans[i][j]][0];
let mut last = ans[i][j];
loop {
if connections[&cur].len() == 2 {
break;
}
let next = connections[&cur]
.iter()
.find(|&&x| x != last && (connections[&x].len() == 3 || connections[&x].len() == 2))
.cloned()
.unwrap();
count_a += 1;
last = cur;
cur = next;
}
let mut count_b = 1;
cur = connections[&ans[i][j]][1];
last = ans[i][j];
loop {
if connections[&cur].len() == 2 {
break;
}
let next = connections[&cur]
.iter()
.find(|&&x| x != last && (connections[&x].len() == 3 || connections[&x].len() == 2))
.cloned()
.unwrap();
count_b += 1;
last = cur;
cur = next;
}
if count_a < count_b {
ans[0][1] = connections[&ans[0][0]][0];
ans[1][0] = connections[&ans[0][0]][1];
}
else {
ans[0][1] = connections[&ans[0][0]][1];
ans[1][0] = connections[&ans[0][0]][0];
}
continue;
}
if ans[i][j] != -1 {
continue;
}
if i == 0 {
for node in &connections[&ans[i][j - 1]] {
if ans[0][j - 2] != *node {
if degree[3].contains(&node) || degree[2].contains(&node) {
ans[i][j] = *node;
} else {
ans[i + 1][j - 1] = *node;
}
}
}
} else {
let set_a = connections[&ans[i - 1][j]].clone();
let mut set_b = vec![];
for (k, l) in vec![(-1, 0), (1, 0), (0, -1), (0, 1)] {
let get_val = |i: i32, j: i32| -> i32 {
if i < 0 || j < 0 || i >= y as i32 || j >= x as i32 {
return -1;
}
ans[i as usize][j as usize]
};
set_b.push(get_val(i as i32 - 1 + k, j as i32 + l));
}
for node in &set_a {
if !set_b.contains(&node) {
ans[i][j] = *node;
}
}
}
}
}
ans
} else {
edges
}
ans
}
}
fn main() {
let sl = Solution::pancake_sort(vec![3, 2, 4, 1]);
let matrix = arr::make_matrix(
"[[0,1],[0,11],[1,8],[1,9],[2,4],[2,8],[2,9],[3,5],[3,10],[4,6],[4,7],[5,6],[5,7],[6,9],[6,10],[9,11],[10,11]]",
);
let sl = Solution::construct_grid_layout(8, matrix);
println!("{:?}", sl);
}