Skip to content

Commit 5343178

Browse files
authored
refactor shimaguni.rs (#210)
1 parent 7ffb3a7 commit 5343178

1 file changed

Lines changed: 10 additions & 21 deletions

File tree

cspuz_rs_puzzles/src/puzzles/shimaguni.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,19 @@ pub fn solve_shimaguni(
1818
let rooms = graph::borders_to_rooms(borders);
1919
assert_eq!(rooms.len(), clues.len());
2020

21-
let mut idx = vec![vec![(usize::MAX, usize::MAX); w]; h];
21+
let mut room_id = vec![vec![usize::MAX; w]; h];
2222
let mut num_black = vec![];
2323
for i in 0..rooms.len() {
2424
let room = &rooms[i];
25-
let mut cells = vec![];
2625
for j in 0..room.len() {
27-
cells.push(is_black.at(room[j]));
28-
idx[room[j].0][room[j].1] = (i, j);
26+
room_id[room[j].0][room[j].1] = i;
2927
}
28+
let cells = is_black.select(room);
3029
let n = solver.int_var(0, room.len() as i32);
3130
solver.add_expr(count_true(&cells).eq(&n));
3231
solver.add_expr(n.ge(1));
3332
num_black.push(n);
34-
let mut graph = graph::Graph::new(room.len());
35-
for j in 0..room.len() {
36-
let (y, x) = room[j];
37-
if y < h - 1 && idx[y + 1][x].0 == i {
38-
graph.add_edge(j, idx[y + 1][x].1);
39-
}
40-
if x < w - 1 && idx[y][x + 1].0 == i {
41-
graph.add_edge(j, idx[y][x + 1].1);
42-
}
43-
}
44-
graph::active_vertices_connected(&mut solver, &cells, &graph);
33+
graph::active_vertices_connected_2d_region(&mut solver, is_black, room);
4534
}
4635
for i in 0..rooms.len() {
4736
if let Some(n) = clues[i] {
@@ -54,15 +43,15 @@ pub fn solve_shimaguni(
5443
let mut adj_rooms = vec![];
5544
for y in 0..h {
5645
for x in 0..w {
57-
if y < h - 1 && idx[y][x].0 != idx[y + 1][x].0 {
58-
let a = idx[y][x].0;
59-
let b = idx[y + 1][x].0;
46+
if y < h - 1 && room_id[y][x] != room_id[y + 1][x] {
47+
let a = room_id[y][x];
48+
let b = room_id[y + 1][x];
6049
adj_rooms.push((a.min(b), a.max(b)));
6150
solver.add_expr(!(is_black.at((y, x)) & is_black.at((y + 1, x))));
6251
}
63-
if x < w - 1 && idx[y][x].0 != idx[y][x + 1].0 {
64-
let a = idx[y][x].0;
65-
let b = idx[y][x + 1].0;
52+
if x < w - 1 && room_id[y][x] != room_id[y][x + 1] {
53+
let a = room_id[y][x];
54+
let b = room_id[y][x + 1];
6655
adj_rooms.push((a.min(b), a.max(b)));
6756
solver.add_expr(!(is_black.at((y, x)) & is_black.at((y, x + 1))));
6857
}

0 commit comments

Comments
 (0)