Sudoku Solver和HiddenSingles Java

时间:2014-11-03 03:20:33

标签: java

所以我正在尝试编写一个解决数独游戏的程序,它大部分时间都可以工作,但是当我尝试解决一个谜题(只能通过hiddenSingles解决)时,它有时会起作用,有时它也不会。

public class Sudoku {

    private int row, column;
    private int[][]board;
    public Sudoku ()
    {

    }
    public Sudoku (int[][]board)
    {
        column = board.length;
        row = board[0].length;
        this.board = board;
    }

    public void printBoard() 
    {
        for (int i = 0; i < 9; i++) {
                        if (i%3==0)
            {
                System.out.println("______________________________");
            }
            for (int j = 0; j < 9; j++) {
                if(j % 3 == 0)
                    System.out.print("|");
                if (j == 0)
                {
                    System.out.printf("" + board[i][j]);
                }
                else {
                    System.out.printf("  " + board[i][j]);
                }
            }

            System.out.print("|");
            System.out.printf("\n");
        }

    }
    public int [][] board()
    {
        return board;
    }
    public boolean[] candidates(int row, int column)
    {

        boolean[] candidate = new boolean[10];
        candidate[0] = false;
        for (int i = 1; i < 10; i++)
        {
            if (!isRowOccupied (i, row, column) && !isColumnOccupied (i, row, column) && !isBoxOccupied (i, boxRow (row), boxColumn(column), row, column))
            {
                candidate[i] = true;
            }
        }
        return candidate;
    }
    public int boxRow (int row)
    {
        int r = 0;
        if (row >= 3 && row <= 5 )
        {
            r = 3;
        }
        if (row >= 6 && row <= 8)
        {
            r = 6;
        }
        return r;
    }
    public int boxColumn (int column)
    {
        int c = 0;
        if (column >= 3 && column <= 5)
        {
            c = 3;
        }
        if (column >= 6 && column <= 8)
        {
            c = 6;
        }
        return c;
    }
    public boolean isRowOccupied (int value, int row, int column)
    {
        for (int i = 0; i < board.length; i++)
        {
            if (i != column && board[row][i] == value)
            {
                return true;
            }
        }
        return false;
    }
    public boolean isColumnOccupied  (int value, int row, int column)
    {
        for (int i = 0; i < board.length; i++)
        {
            if (i != row && board[i][column] == value)
            {
                return true;
            }
        }
        return false;
    }
    public boolean isBoxOccupied (int value, int rowStart, int columnStart, int row, int column)
    {
        for (int i = rowStart; i < rowStart + 3; i++)
        {
            for (int j = columnStart; j < columnStart + 3; j++)
            {
                if (i != row && j != column && board[i][j] == value)
                {
                    return true;
                }
            }
        }
        return false;
    }
    public boolean isSolved()
    {
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (board [i][j] == 0)
                {
                    return false;
                }
            }
        } 
        return true;
    }
    public void solve()
    {
        while (!isSolved() && (hiddenSingles()) && nakedSingles())
        {        
//            boolean [] array = candidates (0, 2);
//            for (int i = 0; i < 10; i++)
//            {
//                 System.out.println (array [i]);
//            }
//           
            printBoard();
        }
    }

    public boolean nakedSingles()
    {
        boolean nakedSingle = false;
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if (board[i][j] == 0)
                {
                    boolean [] array = candidates (i, j);
                    int count = 0, insert = 0;
                    for (int k = 1; k < array.length; k++)
                    {
                        if (array[k])
                        {
                            count++;
                            insert = k;
                        }
                    }
                    if (count == 1)
                    {
                        board [i][j] = insert;
                        nakedSingle = true;     
                    }
                }
            }
        }
        return nakedSingle;
    }
    public boolean hiddenRow()
    {
        boolean hiddenRow = false;
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if (board[i][j] == 0)
                {
                    boolean [] array = candidates (i, j);                 
                    for (int k = 0; k < 9; k++) {
                        if (board[i][k] == 0 && k != j) {

                            boolean [] array2 = candidates (i, k);
                            for (int m = 1; m < array2.length; m++)
                            {
                                if (array [m] == array2 [m] && array [m] == true)
                                {
                                    array [m] = false;
                                }
                            }
                        }
                    }
                    for (int m = 1; m < 10; m++)
                    {
                        if (array [m])
                        {
                            board [i][j] = m; 
                            hiddenRow = true;
                            break;
                        }
                    }
                }
            }

        }
        return hiddenRow;
    }
    public boolean hiddenColumn()
    {
        boolean hiddenColumn = false;
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if (board[i][j] == 0)
                {
                    boolean [] array = candidates (i, j);
                    for (int k = 0; k < 9; k++) {
                        if (board[k][j] == 0 && k != i) {

                            boolean [] array2 = candidates (k, j);
                            for (int m = 1; m < array2.length; m++)
                            {
                                if (array [m] == array2 [m] && array [m] == true)
                                {
                                    array [m] = false;
                                }
                            }
                        }
                    }
                    for (int m = 1; m < 10; m++)
                    {
                        if (array [m])
                        {
                            board [i][j] = m; 
                            hiddenColumn = true;
                            break;
                        }
                    }
                }
            }
        }
        return hiddenColumn;
    }
    public boolean hiddenBox()
    {
        boolean hiddenBox = false;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (board[i][j] == 0)
                {
                    boolean [] array = candidates (i, j);
                    for (int m = boxRow(i); m < boxRow(i) + 3; m++)
                    {
                        for (int n = boxColumn(j); n < boxColumn (j) + 3; n++)
                        {
                            if (board [m][n] == 0 && m != i && n != j) {

                                boolean [] array2 = candidates (m, n);
                                for (int q = 1; q < array2.length; q++)
                                {
                                    if (array [q] == array2 [q] && array [q])
                                    {
                                        array [q] = false;
                                    }
                                }
                            }
                        }
                    }
                    for (int m = 1; m < 10; m++)
                    {
                        if (array [m])
                        {
                            board [i][j] = m; 
                            hiddenBox = true;
                            break;
                        }
                    }
                }
            }
        }
        return hiddenBox;
    }
    public boolean hiddenSingles()
    {
        boolean hiddenSingle = false;
        if (hiddenRow() || hiddenColumn() || hiddenBox())
        {
            hiddenSingle = true; 
        }
//        int [] countCandidates = {0,0,0,0,0,0,0,0,0,0};
//        int insert = 0;
//        boolean hiddenSingle = false;
//        boolean isRow = false;
//        boolean isColumn = false;
//        for(int i = 0; i < 9; i++)
//        {
//            for(int j = 0; j < 9; j++)
//            {
//                if (board[i][j] == 0)
//                {
//                    int count = 0;
//                    boolean [] array = candidates (i, j);
//                    //rows
//                    for (int k = 0; k < 9; k++) {
//                        if (board[i][k] == 0 && k != j) {
//                            
//                            boolean [] array2 = candidates (i, k);
//                            for (int l = 1; l < array2.length; l++)
//                            {
//                                if (array [l] == array2 [l] && array [l])
//                                {
//                                    countCandidates [l] = ++countCandidates [l];
//                                }
//                            }
//                        }
//                    }
//                    for (int l = 1; l < countCandidates.length; l++)
//                    {
//                        if (countCandidates [l] == 0)
//                        {
//                            insert = l;
//                            board [i][j] = insert; 
//                            hiddenSingle = true;
//                            isRow = true;
//                            break;
//                        }
//                    }
//                    if (!isRow)
//                    {
//                        //columns
//                        for (int k = 0; k < 9; k++) {
//
//                            if (board[k][j] == 0 && k != i) {
//                                boolean [] array2 = candidates (k, j);
//                                for (int l = 1; l < array2.length; l++)
//                                {
//                                    if (array [l] == array2 [l] && array[l])
//                                    {
//                                        countCandidates [l] = countCandidates [l]++;
//                                    }
//                                }
//                            }
//                        }
//                        for (int l = 1; l < countCandidates.length; l++)
//                        {
//                            if (countCandidates [l] == 0)
//                            {
//                                insert = l;
//                                board [i][j] = insert; 
//                                hiddenSingle = true;
//                                isColumn = true;
//                                break;
//                            }
//                        }
//                        if (!isColumn)
//                        {
//                            for (int m = boxRow(i); m < boxRow(i) + 3; m++)
//                            {
//                                for (int n = boxColumn(j); n < boxColumn (j) + 3; n++)
//                                {
//                                    if (board [m][n] == 0 && m != i && n != j) {
//                                        boolean [] array2 = candidates (m, n);
//                                        for (int l = 1; l < array2.length; l++)
//                                        {
//                                            if (array [l] == array2 [l] && array [l])
//                                            {
//                                                countCandidates [l] = countCandidates [l]++;
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//
//                            for (int l = 1; l < countCandidates.length; l++)
//                            {
//                                if (countCandidates [l] == 0)
//                                {
//                                    insert = l;
//                                    board [i][j] = insert; 
//                                    hiddenSingle = true;
//                                    isColumn = true;
//                                    break;
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
        return hiddenSingle;
    }
    public static void main(String[] args) {
      //String puzzle = "";
        String puzzle = "0010002000000304000500060001050102070008000400020907080900070008000509000003000700";


        int[][] value = new int [9][9];

        int count = 0;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++) 
            {
                value [i][j] = Integer.parseInt(puzzle.substring(count, count+1));
                count++;
            }
        }
        Sudoku a = new Sudoku(value);
        a.printBoard();
        a.solve();

    }
}

0代表拼图中的空白。 有人可以帮我找到我的HiddenSingles方法中的错误吗?我似乎无法找到它的错误......

由于

0 个答案:

没有答案