0

数独パズル ジェネレーターをコーディングしていますが、プログラムをコンパイルして実行すると、空白のコンソール ターミナルしか表示されません。1時間待っていましたが、まだ空のコンソール端末です。論理エラーが原因なのか、それともまだ処理中なのか疑問に思っています。私のコード全体は非常に長いです。パフォーマンスに問題がある場合、どうすれば最適化できますか

ありがとう

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>

using namespace std;
bool checkrow(int row,int value,int array[][9]);

void producearray(int array[][9]);

bool checksquare(int row,int col,int value,int array[][9]);
bool checkcol(int col,int value,int array[][9]);
void populatearray(int array[][9]);

void printarray(int array[][9]);


int main()
{
int array[9][9];
populatearray( array);
producearray(array);
printarray(array);


system("PAUSE");





}

bool checkrow(int row,int value,int array[][9])// checks the entire row, returns false if any two numbers in the row are the same
{
for (int j=0;j<9;j++)
{
    if (value==array[row][j])
    {
        return false;


    }


}

return true;



}

bool checkcol(int col,int value,int array[][9]) // check if any two numbers in the same column are the same
{
for (int j=0;j<9;j++)
{
    if (value==array[j][col])
    {
        return false;


    }


}

return true;

}

bool checksquare(int row,int col,int value,int array[][9]) //checks if the number within the same square are the same
{
if  ( ( row>=0 &&  row<=2) && (col>=0 && col<=2) )
{
    for (int i=0;i<=2;i++)
    {
        for (int j=0;j<=2;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;


}

 else if  ( ( row>=0 &&  row<=2) && (col>=3 && col<=5) )
{
    for (int i=0;i<=2;i++)
    {
        for (int j=3;j<=5;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}

else if  ( ( row>=0 &&  row<=2) && (col>=6 && col<=8) )
{
    for (int i=0;i<=2;i++)
    {
        for (int j=6;j<=8;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}


 else if  ( ( row>=3 &&  row<=5) && (col>=0 && col<=2) )
{
    for (int i=3;i<=5;i++)
    {
        for (int j=0;j<=2;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}

else if  ( ( row>=3 &&  row<=5) && (col>=3 && col<=5) )
{
    for (int i=3;i<=5;i++)
    {
        for (int j=3;j<=5;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}

 else if  ( ( row>=3 &&  row<=5) && (col>=6 && col<=8) )
{
    for (int i=3;i<=5;i++)
    {
        for (int j=6;j<=8;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}

else if  ( ( row>=6 &&  row<=8) && (col>=0 && col<=2) )
{
    for (int i=6;i<=8;i++)
    {
        for (int j=0;j<=2;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;



}

 else if  ( ( row>=6 &&  row<=8) && (col>=3 && col<=5) )
{
    for (int i=6;i<=8;i++)
    {
        for (int j=3;j<=5;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;
}

 else if  ( ( row>=6 &&  row<=8) && (col>=6 && col<=8) )
{
    for (int i=6;i<=8;i++)
    {
        for (int j=6;j<=8;j++)
        {
            if (array[i][j]== value)
            {
                return false;

            }

        }


    }

    return true;
}






}

void producearray(int array[9][9]) //produces the array
{
bool isrow;
bool iscol;
bool issquare;

for (int i=0;i<9;i++)
{
    for (int j=0;j<9;j++)
    {
        do
        {
        array[i][j]=rand()%9+1;
         isrow=checkrow(i,array[i][j],array);
         iscol=checkcol(j,array[i][j],array);
         issquare=checksquare(i,j,array[i][j],array);




        }
           while(isrow==false || iscol==false || issquare==false);

    }



}

}

void populatearray(int array[][9]) // populate the arary
{
for (int i=0;i<9;i++)
{

    for (int j=0;j<9;j++)
    {
        array[i][j]=0;


    }


}




}

void printarray(int array[][9]) //prints the array
{
for (int i=0;i<9;i++)
{

    for (int j=0;j<9;j++)
    {
        cout<<array[i][j]
            <<"\t";


    }
    cout<<endl;


}



}
4

1 に答える 1

7

それは間違っている。あなたのコードは、すべてのフィールドを 1 つずつランダム化し、衝突があるかどうかをチェックして、有効な数独を生成しようとします。これは、数独の一部がすでに埋められている場合、有効な解がなくなるという問題につながる可能性が最も高いです。このようなデッドロックは常に発生します。既存の数値は競合しませんが、残りを埋める方法はありません。新聞から数独を取り、いくつかのフィールドをランダムに埋めて、すぐに競合しないようにします。ほとんどの場合、数独を完成させることはできなくなります。

この問題は、数独をパズルにするものであり、プログラムはそれを無視します。したがって、失敗するに違いありません (ランダム ジェネレーターが今日、本当に、本当に本当に幸運でない限り)。

数独を生成するためのはるかに優れた方法は、行と列を交換し (常に 3 行/列ブロック内で)、3 行/列のブロック全体を交換し、番号を交換するよりも、既知の有効な数独から開始することです (例: 3 ごとに交換)。すべてのスワップが数独全体を有効に保つように、7 と 7 を同時に 3 と 7 ごとに)。

于 2012-12-10T12:28:12.747 に答える