java - 以编程方式解决数独

标签 java puzzle sudoku

我正在尝试使用 Java 解决数独难题。目前此类无法正确解决数独问题。

此类尝试在 9x9 矩阵中查找 0(空格)并记下 0 在列表中的位置以供以后引用。然后它将使用这些位置来解决那个 0。不幸的是,它似乎没有像我希望的那样工作。

这是我使用的 9x9 矩阵:

0 6 0 1 0 4 0 5 0 
0 0 8 3 0 5 6 0 0 
2 0 0 0 0 0 0 0 1 
8 0 0 4 0 7 0 0 6 
0 0 6 0 0 0 3 0 0 
7 0 0 9 0 1 0 0 4 
5 0 0 0 0 0 0 0 2 
0 0 7 2 0 6 9 0 0 
0 4 0 5 0 8 0 7 0 

在这个 9x9 矩阵中有 51 个 0,但是当它解决这个难题时,出于某种奇怪的原因它附加了 66 个位置。我似乎无法查明问题所在。任何帮助将不胜感激!

这是它吐出的尝试解决方案:

9 6 3 1 8 4 7 5 0 
4 7 8 3 9 5 6 2 0 
2 5 0 7 6 0 8 9 1 
8 9 5 4 3 7 2 1 6 
1 2 6 8 5 0 3 0 9 
7 3 0 9 2 1 5 8 4 
5 8 9 0 7 3 4 6 2 
3 1 7 2 4 6 9 0 8 
6 4 2 5 1 8 0 7 3 

代码:

package com.dc.soduku;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Sudoku {

    int[][] grid = new int[9][9];
    int emptyCell = 0;
    List<String> EmptyCells = new ArrayList<String>();

    public Sudoku() {

        for (int i = 0; i < 9; i++) {
            for (int x = 0; x < 9; x++) {

                Scanner scanner = new Scanner(System.in);

                System.out.println("Row " + i + " Column " + x + " (Enter values from 1-9): ");
                int temp = scanner.nextInt();

                grid[i][x] = temp;
            }

        }

    }

    public Sudoku(int[][] gridInput) {

        grid = gridInput;

    }

    public void emptyCellsChecker() {

        int count = 0;

        EmptyCells.clear();

        for (int i = 0; i < 9; i++) {
            for (int x = 0; x < 9; x++) {

                if (grid[i][x] == 0) {

                    System.out.println("Blank at row " + i + " column " + x + ".");
                    count++;

                    EmptyCells.add(i + "," + x);

                }

            }
        }

        System.out.println("Total number of empty cells: " + count);
        // System.out.print(mm.toString());
        System.out.println((EmptyCells.get(1)).substring(0, 1));
        System.out.println((EmptyCells.get(1)).substring(2, 3));

    }

    public void printSudoku() {

        for (int i = 0; i < 9; i++) {
            for (int x = 0; x < 9; x++) {

                System.out.print(grid[i][x] + " ");

            }

            System.out.println("");

        }

    }

    public void appendCell(int row, int col, int replacement) {

        this.grid[row][col] = replacement;

    }

    public int getCell(int row, int col) {

        return grid[row][col];

    }

    public boolean isEmpty() {

        for (int i = 0; i < 9; i++) {
            for (int x = 0; x < 9; x++) {
                if (grid[i][x] == emptyCell) {
                    return true;
                }
            }
        }

        return false;

    }

    public boolean checkRow(int row, int guess) {

        for (int i = 0; i < 9; i++) {
            if (grid[row][i] == guess) {
                return false;
            }

        }

        return true;

    }

    public boolean checkColumn(int col, int guess) {

        for (int i = 0; i < 9; i++) {
            if (grid[i][col] == guess) {
                return false;
            }

        }
        return true;
    }

    public boolean checkBox(int row, int col, int guess) {

        row = (row / 3) * 3;
        col = (col / 3) * 3;

        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                if (grid[row + r][col + c] == guess) {
                    return false;
                }
            }
        }

        return true;
    }

    public boolean checkGuess(int row, int col, int guess) {

        return (checkRow(row, guess) && checkColumn(col, guess) && checkBox(row, col, guess));
    }

    public void solve() {

        int nEmptyCells = EmptyCells.size();
        String tempR, tempC;
        int tRow, tCol, counter = 0;

        if (isEmpty() == false) {

            System.out.println(
                    "Sudoku has no empty cells, you have either provided a solved sudoku or entered something incorrectly.");

        } else {

            for (int i = 0; i < nEmptyCells; i++) {

                tempR = ((EmptyCells.get(i)).substring(0, 1));
                tempC = ((EmptyCells.get(i)).substring(2, 3));

                tRow = Integer.parseInt(tempR);
                tCol = Integer.parseInt(tempC);

                for (int v = 1; v < 10; v++) {

                    if (checkGuess(tRow, tCol, v) == true) {

                        this.grid[tRow][tCol] = v;

                        counter++;
                        System.out.println("Solved row " + tRow + " column " + tCol + " with " + v);
                    }

                }

            }

        }

        System.out.println("Total appended: " + counter);

    }

}

最佳答案

关于java - 以编程方式解决数独,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078603/

相关文章:

java - 从抽象类调用未声明的 super 方法时获取当前对象属性

Java HashSet 和数据类型 Short,不兼容?

java - 如何将字符串中的值分配给二维数组?

c++ - 使用 C++ 读取带有两个连续分隔符的 csv 文件

java - session.getCurrentSession().createCriteria(MyClass.class) 已弃用如何使用 .addOrder() 现在

java - 如何在storm-project中配置不同的logback.xml?

java - 在没有提示的情况下在 Linux/CentOS 上安装和升级 Java

algorithm - 单一候选人和多个面试官?

php - 从给定的多组集合中找出最佳组合

algorithm - 如何使用独特的解决方案生成数独板