有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数独解算器,而不是回溯解算器

过去几周我一直在做一个数独游戏。 该游戏具有很多功能,如:玩游戏、打印数独、解数独

解算函数使用传统的回溯,但这不是问题所在,问题是我需要游戏能够生成一个人类可解的数独,为此我需要一种方法,能够像人类那样解数独

如果有人能帮我解决这个问题,我将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    大量针对人类玩家的数独解决策略在Andrew Stuart's Sudoku page上得到了很好的展示和解释:

    **Show Possibles**         
    1: Hidden Singles      
    2: Naked Pairs/Triples     
    3: Hidden Pairs/Triples    
    4: Naked Quads     
    5: Pointing Pairs      
    6: Box/Line Reduction      
    **Tough Strategies**  
    7: X-Wing      
    8: Simple Colouring        
    9: Y-Wing      
    10: Sword-Fish         
    11: XYZ Wing       
    **Diabolical Strategies** 
    12: X-Cycles       
    13: XY-Chain       
    14: 3D Medusa      
    15: Jelly-Fish         
    16: Unique Rectangles      
    17: Extended Unique Rect.      
    18: Hidden Unique Rect's       
    19: WXYZ Wing      
    20: Aligned Pair Exclusion         
    **Extreme Strategies**    
    21: Grouped X-Cycles       
    22: Empty Rectangles       
    23: Finned X-Wing      
    24: Finned Sword-Fish      
    25: Altern. Inference Chains   
    26: Sue-de-Coq         
    27: Digit Forcing Chains       
    28: Nishio Forcing Chains  
    29: Cell Forcing Chains        
    30: Unit Forcing Chains        
    31: Almost Locked Sets         
    32: Death Blossom      
    33: Pattern Overlay Method         
    34: Quad Forcing Chains        
    **"Trial and Error"** 
    35: Bowman's Bingo
    

    作为一个相当频繁的玩家,我会将策略11之外的一切判断为“不再有趣”。但这可能是品味的问题

  2. # 2 楼答案

    如果你只需要一个快速的随机数独游戏,你可以使用一种特殊的方法创建一个有效的数独模式,使用我刚才想出的以下算法:

    You initialize an array with a randomized set of the numbers 1 to 9, 
    technically it's easier if you initialize 3 arrays each with 3 length.
    You can have these numbers be randomized, thus create a different sudoku.
    
    [1 2 3] [4 5 6] [7 8 9]
    Then you shift these:
    [7 8 9] [1 2 3] [4 5 6]
    [4 5 6] [7 8 9] [1 2 3]
    
    Then you shift the numbers inside the arrays:
    
    [3 1 2] [6 4 5] [9 7 8]
    Then you shift the arrays themselves again:
    [9 7 8] [3 1 2] [6 4 5]
    [6 4 5] [9 7 8] [3 1 2]
    
    Then you shift the numbers inside the arrays:
    
    [2 3 1] [5 6 4] [8 9 7]
    Then you shift the arrays again:
    [8 9 7] [2 3 1] [5 6 4]
    [5 6 4] [8 9 7] [2 3 1]
    

    最后一组数独表:

    [1 2 3] [4 5 6] [7 8 9]
    [7 8 9] [1 2 3] [4 5 6]
    [4 5 6] [7 8 9] [1 2 3]
    [3 1 2] [6 4 5] [9 7 8]
    [9 7 8] [3 1 2] [6 4 5]
    [6 4 5] [9 7 8] [3 1 2]
    [2 3 1] [5 6 4] [8 9 7]
    [8 9 7] [2 3 1] [5 6 4]
    [5 6 4] [8 9 7] [2 3 1]
    

    这是有效的。之后,你可以拿出一些数字,然后用你已有的算法检查它是有一个解,还是有多个解。如果删除某个数字会产生多个,则可以撤消该数字并结束删除,或者尝试删除另一个数字