C++ 使用std::set和std::map以及std::stack(回溯c+;+;)的数独解算器(无递归)
我的数独代码为空数独找到解决方案。这里我的任务是使用std::map输入一个包含数字的预定义空间。输入如下所示。/sudoku 4 0 2 4代表nxn数独中的n 然后第一个0表示行,然后第0列表示第2个位置 我试图在这里实现它,但我的错误在于,当我选择一个9x9时,它会给出一个带有输入的无限循环。如果我的输入是数组中的最后一列和最后一行,它会只放置数字,即使它们的数字已经在行、列和子正方形中C++ 使用std::set和std::map以及std::stack(回溯c+;+;)的数独解算器(无递归),c++,C++,我的数独代码为空数独找到解决方案。这里我的任务是使用std::map输入一个包含数字的预定义空间。输入如下所示。/sudoku 4 0 2 4代表nxn数独中的n 然后第一个0表示行,然后第0列表示第2个位置 我试图在这里实现它,但我的错误在于,当我选择一个9x9时,它会给出一个带有输入的无限循环。如果我的输入是数组中的最后一列和最后一行,它会只放置数字,即使它们的数字已经在行、列和子正方形中 int main(int argc,char** argv){ if(2>argc){
int main(int argc,char** argv){
if(2>argc){ // checks if size n is availible
std::cout<< "Please Enter a Size of Sudoku to Solve\n";
return 1;
}
float checkarg=(argc-2)%3; //sees if inputs are a multiple of 3
if(checkarg!=0){ // inputs are not a mulitple of 3 fails
std::cout<<"Please enter a size and coordinates of numbers you'd like to set\n";
return 1;
}
int n= atoi(argv[1]); //n of nxn sudoku size
int root = sqrt(n); //the root of size
if (root != sqrt(n)){ // if the size has no root then its not a sudoku problem
std::cout<<"This is not a suitable size for a sudoku\n";
return 2;
}
std::map< std::pair<int,int>,int > insertednumbers;
int savedsquares= (argc-2)/3; //total number of predefined inputted squares
int j= 2;
for(int i = 0;i < savedsquares;i++){// makes map of the inputted squares
int x = atoi(argv[j]);// gets x coordinate
j++;
int y = atoi(argv[j]);// gets y coordinate
j++;
int sq = atoi(argv[j]);// gets subsquare number
j++;
insertednumbers.insert(std::make_pair(std::make_pair(x,y), sq));
}
std::stack<int> solns;
int trial = 1;
std::vector< std::set<int> >rows(n); //used to check if number is in row
std::vector< std::set<int> >cols(n); //used to check if number is in column
std::vector< std::set<int> >squares(n); //used to check if number is in subsquare
while (solns.size()<(n*n)){
int row = solns.size() / n; // tells us what row number is in
int col = solns.size() % n; // tells us what col number is in
int square = col/root + root*(row/root); // tells us what square number is in
if (insertednumbers.count(std::make_pair(row,col))>0){ //checks if coordinate is in map and inserts it
solns.push(insertednumbers[std::make_pair(row,col)]);
rows[row].insert(insertednumbers[std::make_pair(row,col)]);
cols[col].insert(insertednumbers[std::make_pair(row,col)]);
squares[square].insert(insertednumbers[std::make_pair(row,col)]);
} else if((rows[row].count(trial)==0)&&(cols[col].count(trial)==0)&&(squares[square].count(trial)==0)){ // Checks if number is in row,col or subsquare
solns.push(trial);
rows[row].insert(trial);
cols[col].insert(trial);
squares[square].insert(trial);
trial=1;
} else {
trial++;
while(n<trial){ // if the attempt gets greater than n we make the attempt
// equal the previos solution and pop the last one of the
// solutions and erase it in all rows and columns and then
// add 1 to the attempt to check for the next number if applicable.
trial=solns.top();
solns.pop();
int row = solns.size() / n; // tells us what row number is in
int col = solns.size() % n; // tells us what col number is in
int square = col/root + root*(row/root); // tells us what square number is in
rows[row].erase(trial);
cols[col].erase(trial);
squares[square].erase(trial);
trial++;
}
}
}
std::stack<int> printsolns;
int temp;
while(solns.size()>0){//flips the stack from bottom to top
temp=solns.top();
printsolns.push(temp);
solns.pop();
}
for(int rowi=0;rowi<n;++rowi){ //printing solution
for(int coli=0;coli<n;++coli){
std::cout<<printsolns.top()<<" ";
printsolns.pop();
}
std::cout<<"\n";
}
}
int main(int argc,char**argv){
if(2>argc){//检查大小n是否可用
std::coutShouldn(root!=sqrt(n))
检查be(root*root!=n)
?我意识到这不是错误,但它可能应该被修复。不,这是一个nxn数独,但现在问题不是一个设置错误,而是如何通过用户输入给定位置和值的预定义数字我的错误,我现在看到了sqrt(n)返回一个浮点数,root是一个包含截断的sqrt(n)的整数,当比较不等式时,整数被转换为浮点数,然后进行比较。我的建议也会起作用,但不是必需的。我用数组最后一列和最后一行中的输入重现了您描述的问题,我调用了它,就像这些参数“9 0 0 2 8 8 3”和“3”一样在第8行和第8列中,最终的解决方案在同一子方格中包含另外3个。这里有一个链接指向我所做的,我没有更多的时间来处理它,但也许其他人可以从这里找到答案:你在无限循环中遇到了什么问题?