We must check the validity of sudoku by column,row and 3*3 box.
I din't understand the explanation of code given by the book.
//check whether grid[i][j] is valid in the 3-by-3 box
bool isValid(int i,int j,const int grid[][9])
{ for(int row=(i/3)*3;row<(i/3)*3+3;row++)
{
for(int col=(j/3)*3;col<(j/3)*3+3;col++)
{ if(row !=i && col !=j && grid[row][col]==grid[i][j])
{ return false; }
}
}
return false;
}
The explanation is :
For any grid[i][j] in the 3-by-3 box,its starting cell is grid[3*(i/3)][3*(j/3)](i.e.grid[0][6]).
For example,for grid[2][8],i=2,j=8,3*(j/3)=0 and 3*(j/3)=6.
I don't understand this part.
Anyone can spend some time help me explain on this?

New Topic/Question
Reply



MultiQuote



|