3D数组到3D std :: vector [英] 3D Array to 3D std::vector

查看:165
本文介绍了3D数组到3D std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码函数中用3D std :: vector替换了一个3D数组,它进入了一个无限循环。你能给我一个提示,我真的需要使用一个向量而不是一个数组。谢谢:)

我的初始代码是:

  // arr是一个数独表的3D数组,第三维为单元格保持值0到13 
//,当我分配值时,从索引1开始到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 ++){//数字1到12
if(is_working(arr,row,col,arr [row] [i])){//如果我可以把值放在单元格中
arr [row] [col] [0] = arr [row] [col] [i] //使用该值替换单元格的第一个元素
//这里我想使用向量,因为我想使用ac3算法
//并删除那些不满足约束和收缩域大小的值具有较少的值以验证与backtrack

if(sol(arr))return true;

arr [row] [col] [0] = 0;
}
}

return false; //如果不是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
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 if(is_working(vec,row ,col,vec [row] [col] [i])){//与上面的一样,但有更少的值要验证
vec [row] [col] [0] = vec [row] [一世];

if(sol(vec))return true;

vec [row] [col] [0] = 0;
}
}

return false;
}

现在它进入一个无限循环!初始代码没有错误,一个简单的回溯。在我用 vec 替换 arr 之后出现问题。您能给我一些建议,如何用3D替换 arr 向量

解决方案

我已经解决了问题。我使用一个向量矩阵代替一个3D向量, p>

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

解决方案

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

这篇关于3D数组到3D std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆