簡體   English   中英

3D數組到3D std :: vector

[英]3D Array to 3D std::vector

我在我的代碼函數中用3D std :: vector替換了一個3D數組,它進入了一個無限循環。你可以給我一個提示,我真的需要使用一個向量而不是一個數組。謝謝:)
我的初始代碼是:

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

現在它正在進入一個無限循環!初始代碼沒有錯誤,這是一個簡單的回溯。問題出現在我用vec替換arr 。你能給我一些關於如何用3D矢量替換3D arr的建議

你的問題不夠明確。 如果您還可以發布is_working和find_empty的代碼,那么我們將能夠看到您如何獲取行和列的值。 我會把它作為一個評論,但作為一個新成員,沒有足夠的聲譽,我必須把它作為一個答案。 一旦你分享了is_working()和find_empty()的代碼,我就會編輯它

我已經解決了這個問題。我使用了矢量矩陣而不是3D矢量,它很有效:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM