简体   繁体   中英

How do you find out in which sub-grid a 2D array element is located

C++ beginner here. I'm currently trying to make a sudoku solving program, so I have to check whether a value exists in the 9x9 box it's located in.

This is my code for checking if the element follows the rules:

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

My idea was to find out which 9x9 box the coordinates of the current element lead to, then put that 9x9 grid in another 2D array and check if the element's value exists somewhere else in the grid. I don't really know how, though.

The SUDOKU rules require that the digit is used only once:

  • Rule 1: in each row
  • Rule 2: in each column
  • Rule 3: in each 3x3 subgrid of the 9x9 grid

Function follows_rule() checks for a given grid position, if the value would be allowed or not. For the moment it checks only rules 1 and 2. I propose you the following code for rule 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==COLS !!
            return false;
    }
    // Check if another equal value exists in the 3x3 box 
    int sgc = (colIndex / 3) * 3;   // in wich subgrid are we ?
    int sgr = (rowIndex / 3) * 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;
}

You can test the 3x3 verification with the following code:

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;

Accepted answer does not calculate the subgrids correctly , sgc and and sgr needs to be multiplied with 3 too after division to crrectly identify the subgrid vertices

public boolean isValidEntry(char[][] board, int row , int col,char val)
{
    for(int i = 0 ; i < 9 ;i++){
        if(board[row][i] == val)
            return false;
    }
    
    for(int j = 0 ; j < 9 ;j++){
        if(board[j][col] == val)
            return false;
    }
    
    int sgc = col / 3;   // in wich subgrid are we ?
    int sgr = row / 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 (board[(3*sgr) + i][(3*sgc) + j] == val)
                return false;

    }

    return true;
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM