检查矩阵是否为数独 [英] Check is the matrix is sudoku or not

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

问题描述


我尝试了一个使用输入方阵的程序,想要检查它是否是数独. (如果每行,对角线,每列的总和相等,则其数独又称幻方).
我写了所有的类定义和类方法.该程序接受输入,但不提供任何输出.我一直坚持下去.
这是源代码下载链接(271 kB):
http://www.mediafire.com/file/e2gnw4yzezpknc3/MagicSquare.zip [ ^ ]

样本输入方矩阵:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

谢谢!

编辑-
这是代码(无效):
sudoku.h

#include< iostream>
常量  int  maxrow =  4 ,maxcol =  4 ;
课程数独
{
  公共:
     void  initialize();
    布尔 checksum();
  私有:
     int  square [maxrow] [maxcol];
     bool 相等;
     int  sumdg();
     int  sumrow();
     int  sumcol();
}; 




sudoku.cpp

#include " 
#include< iostream>
使用 命名空间 std;
无效 sudoku :: initialize()
{
    /*  上一步:*/
    /*  发布:我们有一个方框,上面有数字*/
     int  i,j;
    cout<< " ;
     for (i =  1 ; i< = maxrow; i ++)
    {
     for (j =  1 ; j< = maxcol; j ++)
    {
    cout<< " <<< i< j<< " ;
    cin>" square [i] [j];
    }
    }
    checksum();
}
 int  sudoku :: sumdg()
{
    /*  上一页:我们拥有元素的平方*/
    /*  发布:返回对角线总和*/
     int  i,j,sum [ 2 ] = { 0  0 };
     for (i =  1 ; i< = maxrow; i ++){
     for (j =  1 ; j< = maxcol; j ++){
    如果(i == j){
    sum [ 1 ] = sum [ 1 ] + square [i] [j];}
    如果((i + j)==(maxrow + 1)){
    sum [ 2 ] = sum [ 2 ] + square [i] [j];}
    }
    }
    如果(sum [ 1 ]!= sum [ 2 ])
    {
        equal ==  false ;
    }
    cout<< sum [ 1 ];
    返回 sum [ 1 ];
}
 int  sudoku :: sumrow()
{
    /*  我们拥有元素的平方*/
    /*  发布:返回总行数*/
     int  i,j,sum [maxrow];
     for (i =  1 ; i< = maxrow; i ++){
        sum [i] =  0 ;}
     for (i =  1 ; i< = maxrow; i ++){
     for (j =  1 ; j< = maxcol; j ++){
        sum [j] = sum [j] +平方[i] [j];
    }
    }
     for (j =  1 ; j< = maxcol; j ++)
    {
     for (i = j + 1; i< = maxcol; i ++)
    {
        如果(sum [j]!= sum [i])
        {
        等于=  false ;
        }
    }
    }
    cout<< sum [ 0 ];
    返回 sum [ 0 ];
}
 int  sudoku :: sumcol()
{
    /*  我们拥有元素的平方*/
    /*  发布:返回的列之和*/
     int  i,j,sum [maxcol];
     for (i =  1 ; i< = maxrow; i ++){
        sum [i] =  0 ;}
     for (j =  1 ; j< = maxcol; j ++){
     for (i =  1 ; i< = maxrow; i ++){
    sum [i] = sum [i] +平方[i] [j];
    }
    }
     for (j =  1 ; j< = maxcol; j ++){
     for (i = j + 1; j< = maxcol; i ++){
        如果(sum [j]!= sum [i])
        {
            equal ==  false ;
        }
    }
    }
    cout<< sum [ 0 ];
    返回 sum [ 0 ];
}
布尔 sudoku :: checksum()
{
    /*  上一页:我们有对角线,行和列的总和*/
    /*   Post:如果对角线,行和列中的每一个相等,则返回true. */
     int  a = sumdg();
     int  b = sumrow();
     int  c = sumcol();
    如果(a == b& a == c&& b == c&&等于== " ;
    } 其他 {
    cout<< " ;
    }
} 




main.cpp

#include <iostream>
#include"sudoku.h"
using namespace std;

int main()
{
    void instructions();
    sudoku check;
    check.initialize();
    //if(check.checksum())
    //{
    //    cout<<"This is a magic square";
    //}
    //else
    //{
    //cout<<"Not a magic square";
    //}
    //return 0;
}
void instructions()
{
    cout<<"Welcome to Sudoku checker."<<endl;
    cout<<"If the sum of each of the rows, columns, and diagonals will be equal,"<<endl;
    cout<<"then it is a magic square else not"<<endl;
    cout<<"Enter the elements of the dquare now."<<endl;
}

解决方案

此外,确定对角线总和的逻辑也被弄乱了.它不会返回正确的值,因此无论如何实际上都没有任何意义.

这应该是非常简单的逻辑,并且使它变得更加复杂.

将其分成两个对角线.第一个对角线非常简单.您应该只有一个for语句,并且要增加一个变量,因为您只是将
中的值相加 [0,0]
[1,1]
[2,2]
[3,3]

第二个对角线稍微有些困难,尽管仍然没有那么困难.您正在将
中的值相加 [0,3]
[1,2]
[2,1]
[3,0]

您的sumrow逻辑也有问题.您创建了一个名为sum的数组,该数组用于存储每一行​​的总和.然后,您创建了两个for循环. i是行数. j是列数.但是然后,您实际上将列加起来,因为您说了sum[j].

检查行是否相等也变得太复杂了.您无需将每行与其他行进行检查.您需要做的就是将第一行与另一行进行对照.如果第一行等于所有其他行,则其他行也等于其他行.

您在sumcol函数中做了同样的事情.

您还将checksum声明为bool函数,但没有返回值.

如果您不打算返回值,则应将其声明为void.你自己的吗?

您不了解数组编号的方式.它们从0开始.这意味着如果声明一个包含2个元素的数组(如sum[2]={0,0};所示,则第一个元素为sum[0],第二个元素为sum[1].

您已尝试访问不存在的数组成员,因为您将它们视为基于1的数组.


Hi,
I tried a program that takes an input square matrix and wanted to check if it''s a sudoku or not. (If the sum of each of the rows, diagonals, columns is equal, then its a sudoku aka a magic square).
I wrote all of the class definitions and class methods. The program takes the input and doesn''t give any output. I have been stuck on this.
Here''s the source download link (271 kB):
http://www.mediafire.com/file/e2gnw4yzezpknc3/MagicSquare.zip[^]

Sample Input square matrix:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Thanks!

Edit--
Here''s the code (not working):
sudoku.h

#include<iostream>
const int maxrow=4,maxcol=4;
class sudoku
{
  public:
    void initialize();
    bool checksum();
  private:
    int square[maxrow][maxcol];
    bool equality;
    int sumdg();
    int sumrow();
    int sumcol();
};




sudoku.cpp

#include"sudoku.h"
#include<iostream>
using namespace std;
void sudoku::initialize()
{
    /*Pre:*/
    /*Post: We have a square box filled with numbers*/
    int i,j;
    cout<<"Enter the elements in each cell: ";
    for(i=1;i<=maxrow;i++)
    {
    for(j=1;j<=maxcol;j++)
    {
    cout<<"a["<<i<<j<<"]= :";
    cin>>square[i][j];
    }
    }
    checksum();
}
int sudoku::sumdg()
{
    /*Pre: We have the square of elements*/
    /*Post: Sum of diagonals is returned*/
    int i,j, sum[2]={0,0};
    for(i=1;i<=maxrow;i++){
    for(j=1;j<=maxcol;j++){
    if(i==j){
    sum[1]=sum[1]+square[i][j];}
    if((i+j)==(maxrow+1)){
    sum[2]=sum[2]+square[i][j];}
    }
    }
    if(sum[1]!=sum[2])
    {
        equality=false;
    }
    cout<<sum[1];
    return sum[1];
}
int sudoku::sumrow()
{
    /*We have the square of elements*/
    /*Post: Sum of rows is returned*/
    int i,j, sum[maxrow];
    for(i=1;i<=maxrow;i++){
        sum[i]=0;}
    for(i=1;i<=maxrow;i++){
    for(j=1;j<=maxcol;j++){
        sum[j] = sum[j] + square[i][j];
    }
    }
    for(j=1;j<=maxcol;j++)
    {
    for(i=j+1;i<=maxcol;i++)
    {
        if(sum[j]!=sum[i])
        {
        equality = false;
        }
    }
    }
    cout<<sum[0];
    return sum[0];
}
int sudoku::sumcol()
{
    /*We have the square of elements*/
    /*Post: Sum of columns of returned*/
    int i,j, sum[maxcol];
    for(i=1;i<=maxrow;i++){
        sum[i]=0;}
    for(j=1;j<=maxcol;j++){
    for(i=1;i<=maxrow;i++){
    sum[i]= sum[i] + square[i][j];
    }
    }
    for(j=1;j<=maxcol;j++){
    for(i=j+1;j<=maxcol;i++){
        if(sum[j]!=sum[i])
        {
            equality=false;
        }
    }
    }
    cout<<sum[0];
    return sum[0];
}
bool sudoku::checksum()
{
    /*Pre: We have the sum of each of the diagonals, rows and columns*/
    /*Post: True if returned if sum of each of the diagonals, rows and columns is equal. */
    int a=sumdg();
    int b=sumrow();
    int c=sumcol();
    if(a==b && a==c && b==c && equality==true)
    {
    cout<<"This is a magic square";
    }else{
    cout<<"This isn't a magic square";
    }
}




main.cpp

#include <iostream>
#include"sudoku.h"
using namespace std;

int main()
{
    void instructions();
    sudoku check;
    check.initialize();
    //if(check.checksum())
    //{
    //    cout<<"This is a magic square";
    //}
    //else
    //{
    //cout<<"Not a magic square";
    //}
    //return 0;
}
void instructions()
{
    cout<<"Welcome to Sudoku checker."<<endl;
    cout<<"If the sum of each of the rows, columns, and diagonals will be equal,"<<endl;
    cout<<"then it is a magic square else not"<<endl;
    cout<<"Enter the elements of the dquare now."<<endl;
}

解决方案

Also, your logic for determining the sum of the diagonals is all messed up. It doesn''t return the right values, and for that matter really doesn''t make any sense anyway.

It should be pretty simple logic and you made it more complicated.

Separate it into the two diagonals. The first diagonal is very simple. You should have a single for statement with one variable that you increment because you''re simply adding up the values in
[0,0]
[1,1]
[2,2]
[3,3]

The second diagonal is a little more difficult, though still not that difficult. You''re adding up the values in
[0,3]
[1,2]
[2,1]
[3,0]

You also have problems with your sumrow logic. You created an array called sum that is meant to store the sum of each row. Then, you created two for loops. i is the number of rows. j is the number of columns. But then, you actually add up the columns because you say sum[j].

You also get too complicated with checking if the rows are equal. You don''t need to check every row against every other row. All you need to do is check the first row against the other. If the first row is equal to all of the other rows, then the other rows are equal to the others as well.

You did the same thing in the sumcol function.

You also declared checksum a bool function, but you don''t return a value.

If you''re not going to return a value, you should declare it as a void.


are you taking a class for this right now or just trying to figure it out on your own?

You don''t understand the way that arrays are numbered. They are 0-based. That means that if you declare an array with 2 elements (as in sum[2]={0,0}; then, the first element is sum[0] and the second element is sum[1].

You have tried to access members of arrays that don''t exist because you are treating them as if they were 1-based.


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

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