Home > front end >  Write an application that determines whether a Sudoku square is valid
Write an application that determines whether a Sudoku square is valid

Time:12-28

Write an application that determines whether a Sudoku square is valid.

I do now know how to read the matrix. The next step I think I can do it. After I enter 9 valors it breaks. How can I ignore spaces while reading the matrix

Example:

For input data:

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

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

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

The console will display:

True

Suggestion 1:

The first step is to read the Sudoku square from the keyboard.

Note: You should allow blank lines and more than one space between the elements on a line of the square so that you can group the numbers in a more readable form (like the square format given as an example in the problem).

The program will return False if a line contains more or less than 9 elements, non-numeric values, numbers less than 1 or greater than 9.

After the complete shopping cart has been read we check that the entered configuration is valid. To do this we take each column, row and 3x3 block of the frame in turn and check that each element from 1 to 9 appears exactly once.

Here is my code:

using System;

namespace Matrix
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] matrix = new int[9, 0];
            for (int i = 0; i < 9; i  )
            {
                for (int j = 0; j < 9; j  )
                {
                    matrix[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
    }
}

CodePudding user response:

Here is your code adapted to get rid of the whitespaces and take complete lines of the SUDOKU matrix as input:

static void Main(string[] args)
{
    

    int[][] matrix = new int[9][];
    for (int i = 0; i < 9; i  )
    {
        Console.WriteLine($"Enter line {i}:");

        string input = Console.ReadLine();
        
        string input_removed_spaces = input.Replace(" ", "");
        // if you want to remove all whitespaces, you can use Regex for example:
        // input_removed_all_whitespaces = Regex.Replace(input, @"\s ", "");

        //Before setting the matrix, obviously you need additional checks, for example if the user enters invalid inputs
        matrix[i] = Array.ConvertAll(input_removed_spaces.ToCharArray(), c => (int)Char.GetNumericValue(c));
    }

    // do stuff with your matrix here
}

As written, you will have to implement additional checks (what if the user enters letters, nothing, to many numbers, ...) but this should get you started

CodePudding user response:

If you want to ignore spaces while reading the matrix, you can refer to the following code:

static void Main(string[] args)
    {
        int[,] matrix = new int[9, 9];
        Console.WriteLine("input:");
        for (int i = 0; i < 9; i  )
        {
            string str = Console.ReadLine();
            string newstr = Regex.Replace(str, @"\s", "");
            for (int j = 0; j < 9; j  )
            {
                matrix[i, j] = Convert.ToInt32(newstr[j])-48;
            }
        }
    }

The following code can determine whether a Sudoku square is valid:

class Program
{
    static void Main(string[] args)
    {
        int[,] matrix = new int[9, 9];
        Console.WriteLine("input:");
        for (int i = 0; i < 9; i  )
        {
            string str = Console.ReadLine();
            string newstr = Regex.Replace(str, @"\s", "");
            for (int j = 0; j < 9; j  )
            {
                matrix[i, j] = Convert.ToInt32(newstr[j])-48;
            }
        }
        if (isValidSudoku(matrix))
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }
    }
    static int N = 9;
    static bool isinRange(int[,] matrix)
    {
        for (int i = 0; i < N; i  )
        {
            for (int j = 0; j < N; j  )
            {
                if (matrix[i, j] <= 0 ||matrix[i, j] > 9)
                {
                    return false;
                }
            }
        }
        return true;
    }
    static bool isValidSudoku(int[,] matrix)
    {
        if (isinRange(matrix) == false)
        {
            return false;
        }
        bool[] unique = new bool[N   1];
        for (int i = 0; i < N; i  )
        {
            Array.Fill(unique, false);
            for (int j = 0; j < N; j  )
            {
                int Z = matrix[i, j];
                if (unique[Z])
                {
                    return false;
                }
                unique[Z] = true;
            }
        }
        for (int i = 0; i < N; i  )
        {
            Array.Fill(unique, false);
            for (int j = 0; j < N; j  )
            {
                int Z = matrix[j, i];
                if (unique[Z])
                {
                    return false;
                }
                unique[Z] = true;
            }
        }
        for (int i = 0; i < N - 2; i  = 3)
        {
            for (int j = 0; j < N - 2; j  = 3)
            {
                Array.Fill(unique, false);
                for (int k = 0; k < 3; k  )
                {
                    for (int l = 0; l < 3; l  )
                    {
                        int X = i   k;
                        int Y = j   l;
                        int Z = matrix[X, Y];
                        if (unique[Z])
                        {
                            return false;
                        }
                        unique[Z] = true;
                    }
                }
            }
        }
        return true;
    }
}
  • Related