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