3

I replaced a 3D array with a 3D std::vector in my code function and it's entering a infinite loop .Could you give me a hint,I really need to use a vector instead an array.Thanks:)
My initial code was:

//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
}

I replace arr with:

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;
}

and now it's entering a infinite loop!The initial code has no errors,it's a simple backtracking.The problem appears after I replace arr with vec.Could you give me some advice on how to replace 3D arr with an 3D vector

9
  • 2
    "infinite loop at compiling" ehmmm what do you mean with this? Commented Aug 21, 2013 at 17:17
  • 1
    Are you sure you want to start your loop with index 1? Commented Aug 21, 2013 at 17:20
  • vector::resize changes the actual content of the vector by inserting or erasing elements from it Commented Aug 21, 2013 at 17:22
  • Have you tried debugging and seeing where the infinite loop occurs ? Commented Aug 21, 2013 at 17:33
  • Yes it occurs in the for Commented Aug 21, 2013 at 18:03

2 Answers 2

1

Your question is not clear enough. If you can also post the code for is_working and find_empty, then we would be able to see how you are getting the values of row and column. I would have put this as a comment but being a new member and not having enough reputations, I have to put this as an answer. I'll edit it once you share the code for is_working() and find_empty()

Sign up to request clarification or add additional context in comments.

2 Comments

The problem was not in the code.The problem was at the data structure,3D vector somehow wasn't modifying his elements leading to an inconsistency in the algorithm
It must have been a problem with the code or how you were setting the values of the vector. But I am glad that you got it working one way or other.
1

I have solved the problem. I used a matrix of vectors instead a 3D vector and it works great :D

maybe this better for 3d one, 4x4x4

std::vector<std::vector<std::vector<double>>> matrix;

matrix.resize(4, std::vector<std::vector<double>>(4,std::vector<double(4)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.