C++ Multiple threaded Sudoku Solution Validator I would like 27 threads created.
ID: 3715907 • Letter: C
Question
C++
Multiple threaded Sudoku Solution Validator
I would like 27 threads created. One for each row,
one for each column and one for each block.
input: txt file with following format
9 rows,
9 numbers in each row separated by space
Output: Valid or not (yes or no answer)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Text file data
--------------------------------------------------------NO Text file---------------------------------------------------------------------------------------
4 2 5 1 8 6 3 7 9
3 1 7 9 5 2 4 8 6
9 8 6 7 4 3 2 1 5
1 6 3 8 7 4 9 5 2
2 7 9 5 3 1 6 4 8
5 4 8 6 2 9 1 3 7
6 5 2 4 1 8 7 9 3
7 9 1 3 6 5 8 2 4
8 3 4 2 9 7 5 1 6
------------------------------------------------------Yes text file------------------------------------------------------------------------------------------
4 2 5 1 8 6 3 7 9
3 1 7 9 5 2 4 8 6
9 8 6 7 4 3 2 1 5
1 6 3 8 7 4 9 5 2
2 7 9 5 3 1 6 4 8
5 4 8 6 2 9 1 3 7
6 5 2 4 1 8 7 9 3
7 9 1 3 6 5 8 2 4
8 3 4 2 9 7 5 6 1
Explanation / Answer
Below code helps you to find where the error in the sudoku and it validates it is right or wrong .
Hope this helps...
Thankyou.. :)
#include <iostream>
typedef char board_type[9][9];
bool verify_block(board_type board, unsigned blockID)
{
unsigned digits[9] = { 0 } ;
const unsigned rowBegin = (blockID / 3) * 3 ;
const unsigned colBegin = (blockID % 3) * 3 ;
for ( unsigned i=0; i<3; ++i )
for ( unsigned j=0; j<3; ++j )
if ( digits[board[rowBegin+i][colBegin+j]-1]++ )
return false ;
return true ;
}
bool subsq_verify(board_type board)
{
bool verified = true ;
for ( unsigned i=0; i<9; ++i )
if ( !verify_block(board, i) )
{
std::cout << "Sub-square " << i+1 << " is not correct. " ;
verified = false ;
}
return verified ;
}
int main()
{
board_type correctBoard =
{
{8,3,5,4,1,6,9,2,7},
{2,9,6,8,5,7,4,3,1},
{4,1,7,2,9,3,6,5,8},
{5,6,9,1,3,4,7,8,2},
{1,2,3,6,7,8,5,4,9},
{7,4,8,5,2,9,1,6,3},
{6,5,2,7,8,1,3,9,4},
{9,8,1,3,4,5,2,7,6},
{3,7,4,9,6,2,8,1,5}
} ;
board_type incorrectBoard =
{
{8,3,5,4,1,6,9,2,7},
{2,9,6,8,5,7,4,3,1},
{4,1,7,2,9,3,6,5,8},
{5,6,9,1,3,4,7,2,2},
{1,2,3,6,7,8,5,4,9},
{7,4,8,5,2,9,1,6,3},
{6,5,2,7,8,1,3,9,4},
{9,8,1,3,4,5,2,7,6},
{3,7,4,9,6,2,8,1,5}
} ;
std::cout << "correctBoard: " ;
if ( subsq_verify(correctBoard) )
std::cout << " verified correct. " ;
std::cout << "incorrectBoard: " ;
if ( subsq_verify(incorrectBoard) )
std::cout << " verified correct. " ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.