开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 484|回复: 6
收起左侧

[易语言] 有人提供好思路吗?消消乐如何快速识别最佳结果啊

[复制链接]
结帖率:74% (55/74)
发表于 2025-12-11 00:34:54 | 显示全部楼层 |阅读模式   广东省潮州市
14精币
比如是4X6的24格地图,一共3种不同,位置按ID从1到24,如下图
1,1,1,2,3,3,1
1,2,3,2,1,3,2
2,3,1,2,3,2,1
3,2,1,2,3,2,2

按id是
1112331=1.2.3.4.5.6

1232132=7.8.9.10.11.12
……

这种如何快速识别最佳消除方案啊?如图中,最佳消除是消除4个2,位置是:4、10、16、22

我自己的方法是从左到右遍历一遍,识别2个相同,有2个相同再判断是否3个相同,再识别4个相同。。


然后再从上到下遍历一遍。。。发现方法非常慢,也非常傻。。。。所以论坛问问大佬们,有好的思路提供参考吗?


补充内容 (2025-12-11 00:38):
支持连消1个、2个、3个、4个(1*4或2*2)、9个(3*3)、16 个(4*4)


回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值
结帖率:74% (55/74)
 楼主| 发表于 2025-12-11 00:37:11 | 显示全部楼层   广东省潮州市
支持连消1个、 2个、3个、4个、 9个、 16 个
回复

使用道具 举报

结帖率:100% (4/4)

签到天数: 1 天

发表于 2025-12-11 01:30:41 | 显示全部楼层   福建省厦门市
消消乐 你点击一个地方 判断周围有没相同颜色,有就消除
回复

使用道具 举报

结帖率:100% (3/3)
发表于 2025-12-11 01:41:51 | 显示全部楼层   北京市北京市
这个就看你数组玩的6不6了,但是你这量很少,耗费不了多久的,所以随便搞搞就行,想要好思路可以去问问AI
回复

使用道具 举报

结帖率:94% (17/18)

签到天数: 2 天

发表于 2025-12-11 01:42:31 | 显示全部楼层   山东省临沂市
兄弟等我,我学完搭建网站就搞这个
回复

使用道具 举报

结帖率:100% (3/3)
发表于 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),仅需调整行列数和检测边界即可。
回复

使用道具 举报

结帖率:98% (63/64)
发表于 2025-12-11 08:38:50 | 显示全部楼层   河南省许昌市
[Python] 纯文本查看 复制代码
def find_best_match(grid, R=4, C=6):
    from itertools import product
    
    # 形状 (h,w) 按消除块数从大到小排序
    shapes = [(4,4), (3,3), (2,2), (4,1), (1,4), (3,1), (1,3), (2,1), (1,2)]
    
    for h,w in shapes:
        for t in [1,2,3]:
            # 构建类型t的二维前缀和
            prefix = [[0]*(C+1) for _ in range(R+1)]
            for i in range(R):
                for j in range(C):
                    prefix[i+1][j+1] = prefix[i+1][j] + prefix[j+1] - prefix[j] + (grid[j]==t)
            
            # 遍历所有可能矩形起点
            for r in range(R-h+1):
                for c in range(C-w+1):
                    # 计算矩形内类型t的个数
                    cnt = prefix[r+h][c+w] - prefix[r][c+w] - prefix[r+h][c] + prefix[r][c]
                    if cnt == h*w:
                        # 找到可消除矩形
                        ids = []
                        for dr in range(h):
                            for dc in range(w):
                                ids.append((r+dr)*C + (c+dc) + 1)
                        return t, ids, h*w
    return None

# 例子
grid = [
    [1,1,1,2,3,3],
    [1,2,3,2,1,3],
    [2,3,1,2,3,2],
    [3,2,1,2,3,2]
]
print(find_best_match(grid))
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表