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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils import (
|
| 2 |
+
to_tuple_set, to_list, is_connected, is_move_connected, build_response, make_step_info
|
| 3 |
+
)
|
| 4 |
+
|
| 5 |
+
# ============================================================
|
| 6 |
+
# 1. HÀM THẾ NĂNG CHÍNH XÁC THEO BÀI BÁO (Strict Potential)
|
| 7 |
+
# ============================================================
|
| 8 |
+
def calculate_weight(c_y, c_z):
|
| 9 |
+
"""Tính trọng số w_c dựa trên tọa độ y và z theo đúng định nghĩa bài báo."""
|
| 10 |
+
if c_z > 1:
|
| 11 |
+
return 17
|
| 12 |
+
if c_z == 1:
|
| 13 |
+
return 16
|
| 14 |
+
# Nếu c_z == 0
|
| 15 |
+
if c_y > 1:
|
| 16 |
+
return 3
|
| 17 |
+
if c_y == 1:
|
| 18 |
+
return 2
|
| 19 |
+
return 1 # c_z = 0 và c_y = 0
|
| 20 |
+
|
| 21 |
+
def exact_potential(blocks_set):
|
| 22 |
+
"""
|
| 23 |
+
Tính tổng thế năng dựa trên công thức: \Pi_C = \sum w_c(x + 2y + 4z)
|
| 24 |
+
"""
|
| 25 |
+
total = 0
|
| 26 |
+
for x, y, z in blocks_set:
|
| 27 |
+
w_c = calculate_weight(y, z)
|
| 28 |
+
total += w_c * (x + 2 * y + 4 * z)
|
| 29 |
+
return total
|
| 30 |
+
|
| 31 |
+
# ============================================================
|
| 32 |
+
# 2. BỘ THỰC THI MACRO-MOVE (Macro-Move Engine)
|
| 33 |
+
# ============================================================
|
| 34 |
+
def apply_macro_move(blocks_set, move_sequence, get_move_type_func):
|
| 35 |
+
"""
|
| 36 |
+
Thực thi một kịch bản chuỗi các bước đi.
|
| 37 |
+
Bỏ qua việc kiểm tra thế năng trung gian, chỉ cần đảm bảo cuối cùng
|
| 38 |
+
chuỗi này là "safe" (tổng thế năng giảm) và không làm đứt gãy hệ thống.
|
| 39 |
+
"""
|
| 40 |
+
curr_blocks = set(blocks_set)
|
| 41 |
+
steps_taken = []
|
| 42 |
+
|
| 43 |
+
for c, e in move_sequence:
|
| 44 |
+
move_type = get_move_type_func(c, e, curr_blocks)
|
| 45 |
+
# Bắt buộc phải là move hợp lệ và duy trì liên thông
|
| 46 |
+
if not move_type or not is_move_connected(curr_blocks, c, e, 3):
|
| 47 |
+
return None, False
|
| 48 |
+
|
| 49 |
+
next_blocks = set(curr_blocks)
|
| 50 |
+
next_blocks.remove(c)
|
| 51 |
+
next_blocks.add(e)
|
| 52 |
+
|
| 53 |
+
steps_taken.append((c, e, move_type, next_blocks))
|
| 54 |
+
curr_blocks = next_blocks
|
| 55 |
+
|
| 56 |
+
return curr_blocks, steps_taken
|
| 57 |
+
|
| 58 |
+
# ============================================================
|
| 59 |
+
# 3. MÔ HÌNH HÓA SUBPILLAR VÀ CÁC OPERATION [a]-[d] TRONG 3D
|
| 60 |
+
# ============================================================
|
| 61 |
+
def get_subpillars_3d(blocks_set):
|
| 62 |
+
"""Tìm tất cả các cột P = <x, y, z_b..z_t>"""
|
| 63 |
+
columns = {}
|
| 64 |
+
for x, y, z in blocks_set:
|
| 65 |
+
columns.setdefault((x, y), []).append(z)
|
| 66 |
+
|
| 67 |
+
subpillars = []
|
| 68 |
+
for (x, y), zs in columns.items():
|
| 69 |
+
zs = sorted(zs)
|
| 70 |
+
start = prev = zs[0]
|
| 71 |
+
for z in zs[1:]:
|
| 72 |
+
if z == prev + 1:
|
| 73 |
+
prev = z
|
| 74 |
+
else:
|
| 75 |
+
subpillars.append({'x': x, 'y': y, 'z_b': start, 'z_t': prev})
|
| 76 |
+
start = prev = z
|
| 77 |
+
subpillars.append({'x': x, 'y': y, 'z_b': start, 'z_t': prev})
|
| 78 |
+
return subpillars
|
| 79 |
+
|
| 80 |
+
def operation_local_z_reduction(blocks_set):
|
| 81 |
+
"""
|
| 82 |
+
Tương đương Operation [a], [b], [c], [d].
|
| 83 |
+
Nhắm vào việc giảm z của các non-cut subpillar.
|
| 84 |
+
"""
|
| 85 |
+
subpillars = get_subpillars_3d(blocks_set)
|
| 86 |
+
|
| 87 |
+
for P in subpillars:
|
| 88 |
+
x, y, z_b, z_t = P['x'], P['y'], P['z_b'], P['z_t']
|
| 89 |
+
|
| 90 |
+
# Bỏ qua nếu P là cut-subpillar (gỡ ra làm đứt hệ thống)
|
| 91 |
+
# Giả định có hàm is_non_cut_subpillar_3d
|
| 92 |
+
if not is_non_cut_subpillar_3d(blocks_set, x, y, z_b, z_t):
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
# Duyệt 4 mặt bên (sides) của P trong 3D: (x-1,y), (x+1,y), (x,y-1), (x,y+1)
|
| 96 |
+
sides = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
|
| 97 |
+
for s_x, s_y in sides:
|
| 98 |
+
# Lấy cột kề (P') cao nhất hoặc thấp nhất ở mặt này
|
| 99 |
+
P_prime = get_adjacent_pillar(blocks_set, s_x, s_y)
|
| 100 |
+
if not P_prime: continue
|
| 101 |
+
|
| 102 |
+
p_z_b, p_z_t = P_prime['z_b'], P_prime['z_t']
|
| 103 |
+
|
| 104 |
+
# [Operation A] Ném đỉnh cột xuống nếu cột kề đủ thấp
|
| 105 |
+
if p_z_t <= z_t - 2:
|
| 106 |
+
c = (x, y, z_t)
|
| 107 |
+
e = (s_x, s_y, z_t - 1) # Rơi chéo qua (convex)
|
| 108 |
+
return "op_a", [(c, e)]
|
| 109 |
+
|
| 110 |
+
# [Operation B] Cột kề có đáy cao hơn đáy P (z_b' > z_b)
|
| 111 |
+
if p_z_b > z_b:
|
| 112 |
+
c_under = (x, y, p_z_b - 1)
|
| 113 |
+
c_base = (x, y, p_z_b)
|
| 114 |
+
|
| 115 |
+
if is_non_cut(blocks_set, c_under) and is_non_cut(blocks_set, c_base):
|
| 116 |
+
return "op_b_normal", [
|
| 117 |
+
(c_under, (s_x, s_y, p_z_b - 1)),
|
| 118 |
+
(c_base, c_under)
|
| 119 |
+
]
|
| 120 |
+
# Nếu bị vướng đỉnh (locked), mở khóa đỉnh trước rồi trượt
|
| 121 |
+
elif not is_non_cut(blocks_set, c_base) and p_z_b == z_t - 1:
|
| 122 |
+
return "op_b_unlock", [
|
| 123 |
+
((x, y, z_t), (s_x, s_y, z_t)), # Unlock (làm tăng thế năng tạm thời)
|
| 124 |
+
(c_under, (s_x, s_y, p_z_b - 1)),
|
| 125 |
+
(c_base, c_under)
|
| 126 |
+
]
|
| 127 |
+
|
| 128 |
+
is_based = (x, y, z_b - 1) not in blocks_set
|
| 129 |
+
|
| 130 |
+
# [Operation C] Cột chạm đáy, đáy kề lún sâu hơn
|
| 131 |
+
if is_based and p_z_b < z_b:
|
| 132 |
+
seq = []
|
| 133 |
+
if is_locked(blocks_set, x, y, z_t):
|
| 134 |
+
seq.append(((x, y, z_t), (s_x, s_y, z_t))) # Unlock
|
| 135 |
+
seq.append(((x, y, z_b), (x, y, z_b - 1))) # Tụt xuống
|
| 136 |
+
return "op_c", seq
|
| 137 |
+
|
| 138 |
+
# [Operation D] Cột chạm đáy, đáy kề bằng nhau
|
| 139 |
+
if is_based and p_z_b == z_b and z_b > 0:
|
| 140 |
+
seq = []
|
| 141 |
+
if is_locked(blocks_set, x, y, z_t):
|
| 142 |
+
seq.append(((x, y, z_t), (s_x, s_y, z_t))) # Unlock
|
| 143 |
+
seq.append(((x, y, z_b), (s_x, s_y, p_z_b - 1))) # Lật qua góc
|
| 144 |
+
return "op_d", seq
|
| 145 |
+
|
| 146 |
+
return None, []
|
| 147 |
+
|
| 148 |
+
# ============================================================
|
| 149 |
+
# 4. LUỒNG THỰC THI CHÍNH (Exact Pipeline)
|
| 150 |
+
# ============================================================
|
| 151 |
+
def compact_3d_exact(blocks, max_steps):
|
| 152 |
+
blocks_set = to_tuple_set(blocks)
|
| 153 |
+
steps = []
|
| 154 |
+
|
| 155 |
+
# 1. Trạng thái khởi tạo
|
| 156 |
+
pot_initial = exact_potential(blocks_set)
|
| 157 |
+
# ... (Lưu info tương tự mã cũ)
|
| 158 |
+
|
| 159 |
+
while len(steps) < max_steps:
|
| 160 |
+
if is_finished_3d(blocks_set):
|
| 161 |
+
break
|
| 162 |
+
|
| 163 |
+
# Thử chuỗi Local z-reduction [a-d]
|
| 164 |
+
op_name, move_seq = operation_local_z_reduction(blocks_set)
|
| 165 |
+
|
| 166 |
+
if move_seq:
|
| 167 |
+
# Thực thi chuỗi vĩ mô thay vì 1 bước
|
| 168 |
+
blocks_set, detailed_steps = apply_macro_move(blocks_set, move_seq, get_move_type_3d)
|
| 169 |
+
if detailed_steps:
|
| 170 |
+
steps.extend(detailed_steps)
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
+
# Thử Pillar Shove [e]
|
| 174 |
+
# op_name, move_seq = operation_pillar_shove(blocks_set)
|
| 175 |
+
# ...
|
| 176 |
+
|
| 177 |
+
# Thử High Components Reduction [f]
|
| 178 |
+
# ...
|
| 179 |
+
|
| 180 |
+
# Nếu duyệt hết các Operation [a]-[j] mà không có move nào -> Bị kẹt
|
| 181 |
+
break
|
| 182 |
+
|
| 183 |
+
# Đóng gói Response...
|
| 184 |
+
return build_response(...)
|