|
|
发表于 2025-12-11 01:48:40
|
显示全部楼层
北京市北京市
要快速识别消消乐的最佳消除方案,核心是结构化建模地图数据 + 定义优先级规则 + 按优先级检测所有消除模式。以下是完整的思路和实现方案,以你提供的 4×6(24 格)地图为例:
一、核心前提:定义 “最佳” 的优先级规则
消消乐的 “最佳” 本质是消除收益最大化,通常按以下优先级从高到低定义(可自定义调整):
优先级 消除类型 消除数量 说明
1 4×4 方块(同色) 16 数量最多,优先级最高
2 3×3 方块(同色) 9 次高数量
3 2×2 方块(同色) 4 方块型优于直线型
4 1×4 直线(竖 / 横) 4 直线型 4 连
5 1×3 直线(竖 / 横) 3 基础消除
6 1×2 直线(竖 / 横) 2 低优先级
7 单个格子 1 兜底(无其他消除时)
二、步骤 1:数据建模(地图结构化 + ID 映射)
将原始地图转换为二维数组,并建立「ID ↔ 行列坐标」的映射(ID 从 1 开始,4 行 6 列):
行索引:0-3(对应第 1-4 行),列索引:0-5(对应第 1-6 列)
ID 计算公式:ID = 行索引×6 + 列索引 + 1
例如:行 0 列 3 → ID=0×6+3+1=4;行 3 列 3 → ID=3×6+3+1=22(对应你说的 4 个 2 的位置)。
三、步骤 2:实现消除模式检测(按优先级从高到低)
按优先级检测所有可能的消除区域,记录「数量、类型、颜色、涉及 ID」,以下是 Python 实现:
python
运行
# ========== 1. 地图数据与坐标映射 ==========
# 修正后的4×6地图(对应ID1-24)
map_data = [
[1, 1, 1, 2, 3, 3], # 行0(ID1-6)
[1, 2, 3, 2, 1, 3], # 行1(ID7-12)
[2, 3, 1, 2, 3, 2], # 行2(ID13-18)
[3, 2, 1, 2, 3, 2] # 行3(ID19-24)
]
ROWS = 4
COLS = 6
# ID转行列:id → (row, col)
def id_to_pos(id_num):
if 1 <= id_num <= ROWS*COLS:
row = (id_num - 1) // COLS
col = (id_num - 1) % COLS
return (row, col)
return None
# 行列转ID:(row, col) → id
def pos_to_id(row, col):
if 0 <= row < ROWS and 0 <= col < COLS:
return row * COLS + col + 1
return None
# ========== 2. 消除模式检测函数 ==========
# 检测4×4同色(16个)
def detect_4x4(map_data):
results = []
for col_start in [0,1,2]: # 4×4列起始仅0/1/2
color = map_data[0][col_start]
is_same, ids = True, []
for row in range(4):
for col in range(col_start, col_start+4):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if not is_same: break
if is_same:
results.append({'count':16, 'type':'4x4', 'color':color, 'ids':ids})
return results
# 检测3×3同色(9个)
def detect_3x3(map_data):
results = []
for row_start in [0,1]: # 3×3行起始仅0/1
for col_start in [0,1,2,3]: # 3×3列起始0-3
color = map_data[row_start][col_start]
is_same, ids = True, []
for row in range(row_start, row_start+3):
for col in range(col_start, col_start+3):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if not is_same: break
if is_same:
results.append({'count':9, 'type':'3x3', 'color':color, 'ids':ids})
return results
# 检测4个同色(2×2方块 / 1×4直线)
def detect_4_group(map_data):
results = []
# 2×2方块(优先级更高)
for row_start in range(ROWS-1):
for col_start in range(COLS-1):
color = map_data[row_start][col_start]
is_same, ids = True, []
for row in range(row_start, row_start+2):
for col in range(col_start, col_start+2):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if not is_same: break
if is_same:
results.append({'count':4, 'type':'2x2', 'color':color, 'ids':ids})
# 1×4竖线(4行全同色)
for col in range(COLS):
color = map_data[0][col]
is_same, ids = True, []
for row in range(4):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if is_same:
results.append({'count':4, 'type':'1x4_vertical', 'color':color, 'ids':ids})
# 1×4横线
for row in range(ROWS):
for col_start in range(COLS-3):
color = map_data[row][col_start]
is_same, ids = True, []
for col in range(col_start, col_start+4):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if is_same:
results.append({'count':4, 'type':'1x4_horizontal', 'color':color, 'ids':ids})
return results
# 检测3个同色(1×3直线)
def detect_3_row(map_data):
results = []
# 1×3横线
for row in range(ROWS):
for col_start in range(COLS-2):
color = map_data[row][col_start]
is_same, ids = True, []
for col in range(col_start, col_start+3):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if is_same:
results.append({'count':3, 'type':'1x3_horizontal', 'color':color, 'ids':ids})
# 1×3竖线
for col in range(COLS):
for row_start in range(ROWS-2):
color = map_data[row_start][col]
is_same, ids = True, []
for row in range(row_start, row_start+3):
if map_data[row][col] != color:
is_same = False
break
ids.append(pos_to_id(row, col))
if is_same:
results.append({'count':3, 'type':'1x3_vertical', 'color':color, 'ids':ids})
return results
# ========== 3. 筛选最佳消除方案 ==========
def find_best_elimination(map_data):
# 按优先级从高到低检测
# 第一步:检测4×4
res_4x4 = detect_4x4(map_data)
if res_4x4:
return sorted(res_4x4, key=lambda x: min(x['ids']))[0]
# 第二步:检测3×3
res_3x3 = detect_3x3(map_data)
if res_3x3:
return sorted(res_3x3, key=lambda x: min(x['ids']))[0]
# 第三步:检测4个同色
res_4 = detect_4_group(map_data)
if res_4:
# 类型优先级:2x2 > 1x4_vertical > 1x4_horizontal
type_prio = {'2x2':3, '1x4_vertical':2, '1x4_horizontal':1}
return sorted(res_4, key=lambda x: (type_prio[x['type']], min(x['ids'])), reverse=True)[0]
# 第四步:检测3个同色
res_3 = detect_3_row(map_data)
if res_3:
return sorted(res_3, key=lambda x: min(x['ids']))[0]
# 兜底:返回第一个2连/单个(示例省略,可补充)
return {'count':1, 'type':'single', 'color':map_data[0][0], 'ids':[1]}
# ========== 4. 测试运行 ==========
best = find_best_elimination(map_data)
print("最佳消除方案:")
print(f"消除数量:{best['count']}")
print(f"消除类型:{best['type']}")
print(f"颜色:{best['color']}")
print(f"涉及ID:{best['ids']}")
四、运行结果(匹配你的示例)
plaintext
最佳消除方案:
消除数量:4
消除类型:1x4_vertical
颜色:2
涉及ID:[4, 10, 16, 22]
五、关键优化与扩展
去重处理:同一区域可能被多次检测(如 3 连包含在 4 连中),可通过记录已检测的 ID 去重;
连消模拟:若需考虑 “消除后新元素下落形成的二次消除”,可模拟消除后的地图,递归调用检测函数;
性能优化:检测时按优先级 “一旦找到高优先级结果就返回”,无需检测低优先级;
自定义规则:可调整类型优先级(如 1×4 竖线比 2×2 优先级高),或增加 “特效触发” 权重(如 4 连触发特效,优先级更高)。
核心逻辑总结
结构化地图数据,建立 ID 与行列的映射;
按 “数量多→方块型优先→位置靠左上” 的优先级检测所有消除模式;
筛选出排序第一的消除方案,即为 “最佳”。
这套逻辑可快速适配不同尺寸的地图(如 5×5、6×6),仅需调整行列数和检测边界即可。 |
|