撰寫一個應用程式來確定數獨方塊是否有效。
我現在知道如何閱讀矩陣了。下一步我想我可以做到。在我輸入 9 valors 后,它會中斷。閱讀矩陣時如何忽略空格
例子:
對于輸入資料:
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
控制臺將顯示:
True
建議1:
第一步是從鍵盤讀取數獨方塊。
注意:您應該允許空行和正方形一行上元素之間的多個空格,以便您可以以更易讀的形式對數字進行分組(如問題中作為示例給出的正方形格式)。
如果一行包含多于或少于 9 個元素、非數字值、小于 1 或大于 9 的數字,程式將回傳 False。
閱讀完完整的購物車后,我們會檢查輸入的配置是否有效。為此,我們依次獲取幀的每一列、每一行和 3x3 塊,并檢查從 1 到 9 的每個元素是否恰好出現一次。
這是我的代碼:
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());
}
}
}
}
}
uj5u.com熱心網友回復:
如果想在讀取矩陣時忽略空格,可以參考以下代碼:
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", "");
if (newstr == "")
{
i--;
}
else
{
for (int j = 0; j < 9; j )
{
matrix[i, j] = Convert.ToInt32(newstr[j]) - 48;
}
}
}
}
以下代碼可以確定數獨方塊是否有效:
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;
}
}
這是我的測驗截圖。

uj5u.com熱心網友回復:
這是您的代碼,用于去除空格并將數獨矩陣的完整行作為輸入:
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
}
正如所寫的,您將不得不實施額外的檢查(如果用戶輸入字母、什么也不輸入、很多數字,...)但這應該會讓您開始
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/396891.html
上一篇:VS創建專案不顯示沒有解決辦法
