0

i have a problem with sudoku.I have to check if is valid or not.I'm stuck at the check line and row, i don't know how to do it.

Here is my code.

        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 
        }

2 Answers2

0

This is my solution for codewars task, but only 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();
    }
}
Aarnihauta
  • 441
  • 3
  • 10
-1

To check for a certain row/column/box you would want a function that takes in what row/column/box to look at, then what number you are checking for. For example, to check for a column (assume 9x9 sudoku)

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
}

assuming you are writing a sudoku solver, you will want to use a backtracking algorithm linked here

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • thanks, but what is this parameter?"int n" i have just one, int[,] sudoku – BogdanNetan Mar 13 '22 at 11:21
  • in this case, n would be the number you want to check. in 3x3 sudoku, if i had a row [1, 2, 4, X] and i passed in 2 for n i would get true because 2 is already present in the row, while passing in 3 would result in false as it is not in the row. – jparker2006 Mar 13 '22 at 11:25