C++ 如何确定二维阵列元素位于哪个子栅格中

C++ 如何确定二维阵列元素位于哪个子栅格中,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,这里是C++初学者。我目前正在尝试制作一个数独解题程序,所以我必须检查它所在的9x9框中是否存在一个值 这是我检查元素是否遵循规则的代码: //constants for rows and columns of the sudoku puzzle, can be changed const int ROWS = 9; const int COLS = 9; bool follows_rule(int grid[ROWS][COLS], int rowIndex, int colIndex, i

这里是C++初学者。我目前正在尝试制作一个数独解题程序,所以我必须检查它所在的9x9框中是否存在一个值

这是我检查元素是否遵循规则的代码:

//constants for rows and columns of the sudoku puzzle, can be changed
const int ROWS = 9;
const int COLS = 9;

bool follows_rule(int grid[ROWS][COLS], int rowIndex, int colIndex, int value){


    for (int i = 0; i < COLS; i++){ 
        if (grid[rowIndex][i] == value) //check if there are any other values on the same column
            return false;
        if (grid[i][colIndex] == value) //or the same row
            return false;
    }
    //to-do: check if another equal value exists in the 9x9 box
    return true;
}

//returns true if another element has the same value as "value", false otherwise
bool exists_in_2d_array(int grid[ROWS][COLS], int value){
    for (int x = 0; x < ROWS / 3; x++)
    {
        for (int y = 0; y < COLS / 3; y++)
        {
            if (grid[x][y] == value)
            {
                return true;
            }
        }
    }
    return false;
}
//数独游戏行和列的常量可以更改
const int ROWS=9;
常数int COLS=9;
bool遵循_规则(int grid[ROWS][COLS],int rowIndex,int colIndex,int value){
对于(int i=0;i

我的想法是找出当前元素的坐标指向哪个9x9框,然后将该9x9网格放在另一个2D数组中,并检查该元素的值是否存在于网格中的其他位置。不过我真的不知道怎么做。

数独规则要求数字只能使用一次:

  • 规则1:每行
  • 规则2:在每列中
  • 规则3:在9x9网格的每个3x3子网格中
函数
遵循规则()
检查给定的网格位置,该值是否允许。目前它只检查规则1和2。我向你提议规则3的以下代码:

bool follows_rule(int grid[ROWS][COLS], int rowIndex, int colIndex, int value){
    for (int i = 0; i < COLS; i++){
        if (grid[rowIndex][i] == value)
            return false;
        if (grid[i][colIndex] == value) // ATTENTION THIS IS OK BECAUSE ROWS==COOLS !!
            return false;
    }
    // Check if another equal value exists in the 3x3 box 
    int sgc = colIndex / 3;   // in wich subgrid are we ?
    int sgr = rowIndex / 3; 
    // check all the elements of the 3x3 grid startic at sgr, sgc
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (grid[sgr + i][sgc + j] == value)
                return false; 
    return true;
}
bool遵循规则(int-grid[ROWS][COLS],int-rowIndex,int-colIndex,int-value){
for(int i=0;i
您可以使用以下代码测试3x3验证:

int sudoku[ROWS][COLS] = {
        { 1, 0, 0, 0, 0, 0, 0, 8, 0 },
        { 0, 0, 2, 0, 0, 0, 0, 0, 0 },
        { 0, 3, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 3, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 5, 0, 0, 0 },
        { 0, 0, 0, 0, 8, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

if (follows_rule(sudoku, 1, 0, 1) == false
    && follows_rule(sudoku, 1, 0, 4) == true
    && follows_rule(sudoku, 5, 5, 8) == false
    && follows_rule(sudoku, 5, 5, 1) == false
    && follows_rule(sudoku, 5, 5, 7) == true)
    cout << "Test ok !" << endl; 
else cout << "Tests failed" << endl;
int数独[行][COLS]={
{ 1, 0, 0, 0, 0, 0, 0, 8, 0 },
{ 0, 0, 2, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 5, 0, 0, 0 },
{ 0, 0, 0, 0, 8, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
如果(遵循_规则(数独,1,0,1)=false
&&遵循规则(数独,1,0,4)=真
&&遵循规则(数独,5,5,8)==false
&&遵循规则(数独,5,5,1)==false
&&遵循规则(数独,5,5,7)==true)

9x9盒子行吗?但这就是整个网格的大小。你确定你不是指网格中的9个3x3盒子吗?我想在那里写3x3盒子,我想我没有注意到。不管怎样,一切都很好。谢谢你花时间。