在我的代码函数中,我用3D std::.Could替换了一个3D数组,它正在进入一个无限循环的array.Thanks--给我一个提示,我真的需要使用一个向量,而不是一个array.Thanks:)
我最初的代码是:
//arr is a 3D array of a sudoku table,the 3 rd dimension is for keeping values 0 to 13
//for a cell, and when I assign values I start from index 1 to 12
bool sol(int arr[12][12][13]) {
int row,col;
if(!find_empty(arr,row,col)) return true;
for(int i=1;i< 12;i++) { //for digits 1 to 12
if(is_working(arr,row,col,arr[row][col][i]) ) { //if i can put the value in a cell
arr[row][col][0] = arr[row][col][i]; //replace the first element for a cell with that value
//here I want to use vector because I want to use an ac3 algorithm
//and remove those values that not satisfy constraints and shrink domain size having less values to verify with backtrack
if(sol(arr)) return true;
arr[row][col][0] = 0;
}
}
return false;//if not backtrack
}我将arr替换为:
std::vector<std::vector<std::vector<int> > > vec;
vec.resize(12);
for(int i=0;i<12;i++)
{
vec[i].resize(12);
for(int j=0;j<12;j++)
{
vec[i][j].resize(13);
for(int k=0;k<13;k++)
vec[i][j][k]=table[i][j][k];
}
}
bool sol(std::vector<std::vector<std::vector<int> > >& vec) {
int row,col;
if(!find_empty(vec,row,col)) return true;
for(int i=1;i< vec[row][col].size();i++) {//for remainig values in domain
if(is_working(vec,row,col,vec[row][col][i]) ) {//same as above but having less values to verify for
vec[row][col][0] = vec[row][col][i];
if(sol(vec)) return true;
vec[row][col][0] = 0;
}
}
return false;
}现在它进入了一个无限循环!初始代码没有错误,这是一个简单的backtracking.The问题,在我将arr替换为vec.Could之后,给我一些关于如何用3D向量替换3D arr的建议。
发布于 2013-08-22 09:47:25
我已经解决了这个问题。我使用了一个向量矩阵,而不是一个三维向量,它工作得很好:D
也许这对3d 1,4x4x4更好
std::vector<std::vector<std::vector<double>>> matrix;
matrix.resize(4, std::vector<std::vector<double>>(4,std::vector<double(4)));发布于 2013-08-22 02:06:29
你的问题不够清楚。如果您还可以发布is_working和find_empty的代码,那么我们将能够看到如何获取行和列的值。我本来会把这句话当作评论,但作为一名新议员,又没有足够的声誉,我必须把这句话作为答案。一旦您共享了is_working()和find_empty()的代码,我将编辑它
https://stackoverflow.com/questions/18363729
复制相似问题