数独正确性检查 [英] Sudoku correctness checking

查看:296
本文介绍了数独正确性检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在大学学习c ++模块,并获得了一项任务。因为我没有关于这个主题的先前背景,所以对我来说一切都是新的。我已经尝试了很长时间,仍然无法解决问题。所以我希望你们能帮助我。当然我并不期待一个完整的解决方案,但如果有人能告诉我该怎么办,我会非常感激。我对这个主题很新,所以如果你有时间,请尽可能多地帮助我。我在这里提出问题以及我的半工作答案。请看一看。谢谢!

作业


作业1,说明


问题:编写一个C ++程序,检查是否正确填充了9x9 Sudoku方块。数独广场中的

81数字必须从文本文件中读取,文本文件包含数字

逐行,用空格分隔。


说明:

Sudoku规则在附带的Sudoku规则文档中有解释。在这个

文件中,我们称为数独方形区域的3x3子方格。


方形保存在向量< int>中。 S有81个条目,这样第i行和

列j的条目是S [9 *(i-1)+ j-1]。


?编译并运行Assignment1.cpp。它将从文件sud1.txt

(您应该已保存)中读取数独广场,并在屏幕上打印

消息,指示输入是否有效。


?关键部分:如果根据规则填写数独广场,则使用C ++命令检查

。详情:


?每当任何数字1,2,...,9在任何行,列或区域中出现不止一次时,应将此事实打印到屏幕上(通过cout命令) 。


?如果方块完全正确,则不应在屏幕上打印任何内容。


?您的程序将在进一步的示例中进行测试。因此,建议您进行更多测试以确保您的程序正确。


?以下程序包含一个基本概念,用于检查数字是否出现在几个数字中最少两次的
。你可以使用这个想法(但不是程序本身 - 它必须调整
以适应目的)。

#include< cstdlib>

#include< iostream>

使用命名空间std;

//生成30个随机数

//范围1,...,100并检查哪一个

//数字多出现一次

int main()

{

bool HasOccurred [101];

// HasOccurred [i] == true意味着

//我已经发生了

for(int i = 0; i< 101; i ++)

HasOccurred [i] = false;

int number;

for(int j = 0; j< 30; j ++)

{

number = 1+ rand()%100;

if (HasOccurred [number])

cout<<数字<< "发生两次 << endl;

HasOccurred [number] = true;

}

system(PAUSE);

返回EXIT_SUCCESS;

}


我的半解决方案:


#include< cstdlib>

#include< iostream>

#include< fstream>

#include< vector>


使用命名空间std;


char file [] =" sud1.txt";


int main(int argc,char * argv [])

{

cout<<档案<< ":" << endl<< endl;

ifstream in(" C:\Documents and Settings\HP\Desktop\assignment\sud1.txt");

vector< int> ; S;

int buffer;

while(在>>>缓冲区)

S.push_back(缓冲区);

if(S.size()== 81)

cout<< 输入工作 << endl;

else

cout<< 输入错误 << endl;

int counter = 0;

for(int i = 1; i< 4; i ++)&& for(int i = 4; i< 7 ; i ++)&& for(int i = 7; i< 10; i ++))

{

for(int j = 1; j< 4; j ++ )&& for(int j = 4; j< 7; i ++)&& for(int j = 7; j< 10; i ++))

{

bool S [9];

for(int i = 1; i< 10; i ++)

{

for(int j = 1; j< 10; i ++)

if(S [i])

counter ++;

}

}

cout<< A [i]已经发生<<计数器<< "倍" <<对于((int i = 1; i< 10; i ++)

{

for(int j = 1; j< 10; j ++)

{

bool S [9];

for( int i = 1; i< 10; i ++)

{

for(int j = 1; j< 10; i ++)

if (S [i])

counter ++;

}

}

cout<<" A [ i]已经发生<<< counter<<" times<<< endl;

}



系统(PAUSE);

返回EXIT_SUCCESS;

}


我的回答对你来说可能看起来很愚蠢,但这个几个小时之后我能想出什么。

Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite some time and still not able to get the solution out. so i hope you guys can help me out. of course i am not expecting a full solution, but i would greatly appreciate it if anyone can suggest to me what should i do. i am very new to this subject so please help me out as much as you can if you have the time. i have put the questions as well as my half-worked answers here. please have a look. thanks!

the assignment:

Assignment 1, Instructions

Problem: Write a C++ program that checks if a 9x9 Sudoku square is filled in correctly. The
81 numbers in the Sudoku square have to be read from a textfile which contains the numbers
row-wise, separated by spaces.

Instructions:
The Sudoku rules are explained in the attached document on the Sudoku rules. As in this
document, we call the 3x3 subsquares of a Sudoku square regions.

The square is saved in a vector<int> S with 81 entries such that the entry in row i and
column j is S[9*(i-1)+j-1].

? Compile and run Assignment1.cpp. It will read the Sudoku square from the file sud1.txt
(which you should have saved) and print a
message to the screen indicating if the input has worked.

? Crucial part: put C++ commands to check
if the Sudoku square is filled in according to the rules. Details:

? Whenever any number 1,2,...,9 appears more than once in any row, column, or region of
the square, this fact should be printed to the screen (by a cout command).

? If the square is completely correct, nothing should be printed to the screen.

? You program will be tested on further examples. Therefore, it is recommended that you
do more tests to make sure your program is correct.

? The following program contains a basic idea needed for checking if a number occurs at
least twice among several numbers. You can use this idea (but not the program itself - it
has to be adjusted to fit the purpose).
# include <cstdlib >
# include <iostream >
using namespace std;
// generate 30 random numbers in
// the range 1 ,... ,100 and check which
// numbers appears more that once
int main ()
{
bool HasOccurred [101];
// HasOccurred [i]== true will mean that
// i has occurred already
for(int i=0;i <101; i++)
HasOccurred [i]= false ;
int number ;
for(int j=0;j <30; j++)
{
number = 1+ rand ()%100;
if( HasOccurred [ number ])
cout << number << " occurred twice " << endl ;
HasOccurred [ number ]= true ;
}
system (" PAUSE ");
return EXIT_SUCCESS ;
}


my half-worked on solution:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

char file[] = "sud1.txt";

int main(int argc, char *argv[])
{
cout << file << ":" << endl << endl;
ifstream in("C:\Documents and Settings\HP\Desktop\assignment\sud1.txt");
vector<int> S;
int buffer;
while(in>>buffer)
S.push_back(buffer);
if(S.size()==81)
cout << "input worked" << endl;
else
cout << "input error" << endl;
int counter=0;
for(int i=1;i<4;i++)&&for(int i=4;i<7;i++)&&for(int i=7;i<10;i++))
{
for(int j=1;j<4;j++)&&for(int j=4;j<7;i++)&&for(int j=7;j<10;i++))
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}

for((int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}


system("PAUSE");
return EXIT_SUCCESS;
}

my answer might seem stupid to you, but this what i can come up with after many hours.

推荐答案

你可以根据数独的属性直接检查。有一个数组来计算每个的出现次数数字(1..9)。你遍历所有的行。所有的列。以及所有3X3的正方形。对于e ach行,列和3X3 sqaure分开,看到所有数字的计数值正好为1.


我认为以下逻辑也可能有效。检查所有行中所有元素的总和是否为45,并且所有列中所有元素的总和是45.还为3X3方块执行此操作。我不确定这个算法的证明。


这个想法只是检查它是不是数独。其余部分很容易
You can directly check according to the property of a sudoku. Have an array that counts the number of occurrences of each number( 1..9 ). You traverse all the rows. All the columns. And all the 3X3 squares too. For each row, column and a 3X3 sqaure seperately, see that the count value for all the numbers is exactly 1.

I think the following logic might also work. Check whether the sum of all the elements in all rows is 45 and also the sum of all the elements in all columns is 45. Do this for 3X3 square also. I am not sure about the proof of this algorithm.

The idea is only to check whether it is a sudoku or not. The rest part is easy



我认为以下逻辑也可能有效。检查所有行中所有元素的总和是否为45,并且所有列中所有元素的总和是45.还为3X3方块执行此操作。我不确定这个算法的证明。
I think the following logic might also work. Check whether the sum of all the elements in all rows is 45 and also the sum of all the elements in all columns is 45. Do this for 3X3 square also. I am not sure about the proof of this algorithm.



这不起作用;反例:一行中包含全部5个。


亲切的问候,


Jos


ps。在Java Howtos部分有一篇关于Sudoku解决的文章;程序中的逻辑

也适用于这个问题。

That doesn''t work; counter example: a row with all 5s in it.

kind regards,

Jos

ps. there''s an article about Sudoku solving in the Java Howtos section; the logic
in the program also applies to this problem.


你不能拥有所有5'的行

每行,每列和9个小方块必须包含数字1到9.

因此每行和每列必须总和为45,每个次要3x3平方必须为。
You can''t have a row of all 5''s
Every row, every column and the 9 minor squares must contain digits 1 to 9.
Therefor every row and column must sum to 45 as must each minor 3x3 square.

这篇关于数独正确性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆