【问题标题】:Check if sudoku is valid检查数独是否有效
【发布时间】:2022-08-07 18:45:27
【问题描述】:

我的数独有问题。我必须检查是否有效。我卡在检查线和行,我不知道该怎么做。

这是我的代码。

        static int[] ReadValues()
        {
            string[] line = Console.ReadLine().Split(\' \');
            int[] array = Array.ConvertAll(line, int.Parse);

            return array;
        }

        static int[,] CreateMatrix()
        {
            const int matrixSize = 9;
            int[,] sudoku= new int[matrixSize, matrixSize];
            for (int i = 0; i < matrixSize; i++)
            {
                int[] array = ReadValues();
                for (int j = 0; j < matrixSize; j++)
                {
                    sudoku[i, j] = array[j];
                }
            }

            return sudoku;
        }


        static bool CheckLine(int[,] sudoku)
        {
            // this is the method where  I\'m stuck 
        }

static bool CheckRow(int[,] sudoku)
        {
            // this is the method where  I\'m stuck 
        }

标签: c#


【解决方案1】:

这是我的代码战任务解决方案,但只有 9x9

public class Sudoku
{
    public static bool ValidateSolution(int[][] board)
    {
        for (int i = 0; i < 9; i++)
        {
            var line = GetLine(board, i, 9).Distinct();
            var column = GetColumn(board, i, 9).Distinct();

            if (line.Count() != 9 || column.Count() != 9)
                return false;
        }
        for (int i = 0; i <= 6; i += 3)
        {
            var block = Get3x3Block(board, i, i).Distinct();

            if (block.Count() != 9)
                return false;
        }
        return true;
    }

    private static int[] GetLine(int[][] arr, int index, int count)
    {
        return arr[index].Take(count).ToArray();
    }
    private static int[] GetColumn(int[][] arr, int index, int count)
    {
        int[] result = new int[count];

        for (int i = 0; i < count; i++)
            result[i] = arr[i][index];

        return result;
    }
    private static int[] Get3x3Block(int[][] arr, int index, int indentLeft)
    {
        var firstLine = GetLine(arr, index, 9).Skip(indentLeft).Take(3);
        var secondLine = GetLine(arr, index + 1, 9).Skip(indentLeft).Take(3);
        var thirdLine = GetLine(arr, index + 2, 9).Skip(indentLeft).Take(3);

        return firstLine.Concat(secondLine).Concat(thirdLine).ToArray();
    }
}

【讨论】:

    【解决方案2】:

    要检查某个行/列/框,您需要一个函数来查看要查看的行/列/框,然后检查您要检查的数字。例如,检查一列(假设 9x9 数独)

    bool checkColumn(int n_col, int n)
    {
        for (int row = 0; row < 9; row++)
        {
            if (n == sudokuMatrix[row][n_col])
                return true;
        }
        return false; // col doesnt have number
    }
    

    假设您正在编写数独求解器,您将需要使用链接here 的回溯算法

    【讨论】:

    • 谢谢,但是这个参数是什么?“int n”我只有一个,int [,]数独
    • 在这种情况下, n 将是您要检查的数字。在 3x3 数独中,如果我有一行 [1, 2, 4, X] 并且我为 n 传入了 2,我会得到 true,因为 2 已经存在于行中,而传入 3 会导致 false,因为它不在行。
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2013-06-24
    • 2015-04-11
    • 2014-05-08
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多