gpt4 book ai didi

c++ - 生成数独谜题所需的时间

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:40 25 4
gpt4 key购买 nike

<分区>

我正在编写一个数独谜题生成器,但当我编译和运行该程序时,我看到的只是一个空白的控制台终端。我已经等了 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;


}



}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com