You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何用临时变量存储get_node调用结果会导致数独连接结果异常?

数独游戏3x3区域连接异常问题

我正在开发一款数独游戏,每个方格需要关联其所在行、列以及3x3区域内的所有其他方格。在Sudoku构造函数中负责建立这些方格间的连接,行和列的连接逻辑完全正常——需要建立1296个连接,完成后打印的count值恰好为1296,符合预期。

但在处理3x3区域的连接时出现了异常:

  • 使用option2实现时,最终count值为324,打印棋盘能得到预期结果
  • 将get_node函数的调用结果存储到临时变量(即option1)时,count值变为648,且每个方格的连接数不正确(应为20却得到16)

get_node函数仅执行return _board[(I * dim) + J]操作,按理说不应引发问题,有人知道该问题的成因吗?

提前感谢!

附:我暂时禁用了编译器警告以让goto语句通过编译——这并非问题所在,只是为了让代码更易读 :)

// areas
int count(0);
for (int a = 0; a < 3; ++a) {
    for (int b = 0; b < 3; ++b) {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                for (int m = 0; m < 3; ++m) {
                    for (int n = 0; n < 3; ++n) {
                        if (i == m && j == n) continue;
                        
                        goto option1;
                        
                        //------------------------
                        
                        option1:
                        
                        {
                        auto current_node = get_node(a*3 + i, b*3 + j);
                        auto potential_connection = get_node(a*3 + m, b*3 + n);
                        
                        if (ISINSIDE(current_node.get_connections(), &potential_connection)) continue;
                        current_node.add_connection(&potential_connection);
                        
                        goto end;
                        }
                        
                        //------------------------
                        
                        option2:
                        {
                        
                        if (ISINSIDE(get_node(a*3 + i, b*3 + j).get_connections(), &get_node(a*3 + m, b*3 + n))) continue;
                        get_node(a*3 + i, b*3 + j).add_connection(&get_node(a*3 + m, b*3 + n));
                        
                        goto end;
                        }
                                                    
                        //------------------------
                        
                        end:
                        count += 1;
                        std::cout << count << '\n';
                    }
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者appelflap796

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:13:24