【问题标题】:How to eliminate array item in array 3 dimension java如何消除数组3维java中的数组项
【发布时间】:2017-07-22 14:03:14
【问题描述】:

我在消除单元格数独中的候选者时遇到问题,我试图搜索候选者来填充如下所示的空单元格

for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++)
            if(game.getNumber(x, y) == 0){
                for(int z = 1; z <=9; z++){
                    if (CekRow(game, y, z) && CekColumn(game, x, z) && CekRegion(game, x, y, z)){
                        list[y][x][z-1] = z;
                    }
                }
            }
    }

然后在找到空单元格并填充候选人后,我想消除可能的项目数组,如方法 enter image description here。我有人可以提供消除数组列表[y][x][z]的解决方案。

【问题讨论】:

  • 消除是指使数组的大小更短吗?
  • CekRow(game, y, z) ...等是什么?你是什​​么意思消除?
  • CekRow 可能会检查值 z 在行 y 中是否存在(或可能)。
  • 我认为从已经设置的值开始并消除受影响的行、列和区域的可能性,而不是从未设置的值开始并尝试填充它们可能更有效率。
  • 我的意思是第 1 行有可能像这样 0 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 0 | 0 0 0 0 5 6 0 0 9 | 0 0 3 4 5 0 0 0 9 | 0 0 0 0 0 0 0 0 0 | 0 0 0 4 0 6 0 0 9 | 0 0 0 0 5 0 0 0 9 | 0 0 0 0 0 0 0 0 0 | 0 0 0 0 5 0 0 0 9 导致第 7 列和第 9 列的单元格中有可能的项目(5 和 9),我想消除数组上可能的项目(5 和 9)其他等(5,6,9)->( 0,6,0)

标签: java arrays divide-and-conquer


【解决方案1】:

这是使用 Java BitSet 类的数独板的另一种可能的数据结构。板上的每个单元格都有其可能性,由名为 possibilitiesBitSet 变量跟踪。

还显示了一个示例eliminateInRow 方法,它从一行中消除数字作为可能性。该方法包括要在行中跳过的列列表,以免除最初导致我们进行消除的单元格(例如,OP 在原始问题的 cmets 中提到的单元格 7 和 9)。还有另一种情况,如果您发现 3 个单元格都包含可能性 (3,5,9)(例如),那么您可以从该行中的所有其他单元格中消除 3,5,9 作为可能性(排除原始的 3 个单元格)。此方法将同时处理 2-case 和 3-case,因为它使用整数 List 表示要跳过的列。

import java.util.BitSet;
import java.util.List;

class SudokuCell {
    BitSet possibilities;
    CellValue value;

    SudokuCell() {

        // Initially the value of this cell is undefined
        value = CellValue.Undefined;

        possibilities = new BitSet(9);
        // Initially every value is possible
        possibilities.set(0, 9);
    }

    // clears the possibility of 'number' from this cell
    void clear(int number) {
        possibilities.clear(number - 1);
        // If there is only one possibility left
        if (possibilities.cardinality() == 1) {

            // Set value of the cell to that possibility
            int intValue = possibilities.nextSetBit(0);
            value = CellValue.fromInteger(intValue);
        }
    }

    // returns the value of this square if it is defined
    int getValue() throws Exception {
        if (value == CellValue.Undefined) {
            throw new Exception("Undefined cell value");
        }
        return CellValue.toInteger(value);
    }

    // returns whether 'num' is still possible in this cell
    boolean isPossible(int num) {
        return possibilities.get(num-1);
    }
}

public class SudokuBoard {
    SudokuCell[][] sudokuBoard;

    // constructor
    SudokuBoard() {
        sudokuBoard = new SudokuCell[9][9];
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                sudokuBoard[row][col] = new SudokuCell();
            }
        }

    }

    // eliminate the possibility of 'num' from row 'row'
    // skipping columns in List skipCols
    void eliminateInRow(int row, int num, List<Integer> skipCols) {
        for (int col = 0; col < 9; col++) {
            if (!skipCols.contains(col)) {
                sudokuBoard[row][col].clear(num);
            }
        }

    }

}

下面是enumCellValue的代码,是可选的:

enum CellValue {
    Undefined, One, Two, Three, Four, Five, Six, Seven, Eight, Nine;
    public static CellValue fromInteger(int x) {
        switch (x) {
        case 0:
            return Undefined;
        case 1:
            return One;
        case 2:
            return Two;
        case 3:
            return Three;
        case 4:
            return Four;
        case 5:
            return Five;
        case 6:
            return Six;
        case 7:
            return Seven;
        case 8:
            return Eight;
        case 9:
            return Nine;
        }
        return null;
    }

    public static int toInteger(CellValue value) throws Exception {
        switch (value) {
        case Undefined:
            throw new Exception("Undefined cell value");
        case One:
            return 1;
        case Two:
            return 2;
        case Three:
            return 3;
        case Four:
            return 4;
        case Five:
            return 5;
        case Six:
            return 6;
        case Seven:
            return 7;
        case Eight:
            return 8;
        case Nine:
            return 9;
        }
        throw new Exception("Undefined Cell Value");
    }
}

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多