FRESHERSHOME![]() |
This is a discussion on C program help with sudoku puzzle routine to solve? within the Puzzles forums, part of the Freshers Zone category; We are one routine short of solving the Sudoku. That routine is a recursive routine called ProcessCell. Here is the ...
| |||||||
| Notices |
| Puzzles A puzzle is a problem or enigma that challenges ingenuity. Puzzles are often contrived as a form of entertainment, but they can also stem from serious mathematical or logistical problems, All Puzzle related Information and q&a |
![]() |
| | Thread Tools | Display Modes |
|
#1
| |||
| |||
| We are one routine short of solving the Sudoku. That routine is a recursive routine called ProcessCell. Here is the pseudocode for ProcessCell, implement it in C: /* Add these definitions to the top of your source module */ #define FIRSTATTEMPT(1) #define LASTATTEMPT(9) ProcessCell (CELL aCells[][SUDOKULIMIT], int nRow, int nCol) { Set nOrgCell flag to indicate if this cell was set in the puzzle definition (aCell[nRow][nCol].m_nOrgVal != 0) Define and initialize nLastCell flag to FALSE Print a message indicating the row and column we are processing. Start the message with \r so we stay on the same line if GetNextCell(nRow, nCol, &nNextRow, &nNextCol) returns FALSE then nLastCell = TRUE if this an original cell (nOrgCell != FALSE), then if this is the last cell (nLastCell != FALSE) then return TRUE else return the result of a recusive call to ProcessCell with the next row and next column /* At this point we know it's not an original cell (i.e. not in the puzzle definition) */ set nAttempt to FIRSTATTEMPT Loop while nAttempt <= LASTATTEMPT { if nAttempt is in current row, column, or subgrid (use Check...) then continue; /* The current value of nAttempt is ok for this cell for now */ put nAttempt in the current value for this cell if this is the last cell then return TRUE -- we solved the puzzle!! Call ProcessCell recursively with nNextRow and nNextCol if the recursive ProcessCell call returns TRUE then return TRUE -- we solved the puzzle!! /* other wise we'll continue with the next value for nAttempt */ Your loop should increment nAttempt each time through }// End of nAttempt loop /* If we exit the loop then no number from FIRSTATTEMPT to * LASTATTEMPT worked for this cell. */ Set current value for cell back to 0. /* Tell the caller that a cell prior to this one must change */ return FALSE -- we're not able to solve the puzzle with the values that are currently in the previous cells } // end of ProcessCell To solve the puzzle you must start with a call to ProcessCell in main for the first cell in row-wise order: void main() { Declare and initialize stuff LoadArray PrintArray nResult = ProcessCell(aCells, 0, 0) if (nResult) { PrintArray } else { Print an "unsolvable" message } }
__________________ Powered by Yahoo! |
![]() |
| Tags |
| sudoku |
| Thread Tools | |
| Display Modes | |
|
|
Sitemap
Jobs by Location: Advertising and Marketing Jobs - IT Software Jobs - Walk-in Jobs - BPO Jobs - Government Jobs - Sales / BD Jobs - Tele Communication Jobs App Programming - Network Admin
Jobs By Location: Jobs in Bangalore - Jobs In India - Jobs in Delhi - Jobs in Hyderabad - Jobs in Kochi - Jobs in Mumbai - Jobs in Trivandrum - Jobs in pune - Jobs in Jonida - Jobs in Chennai - Jobs in Coimbator
Jobs Type: Full Time Jobs - Part Time Jobs
Latest Jobs - Accounting Jobs - Engineering Jobs - IT Jobs - Walkins - How to Face Interview - HR Round Tips - Career Info - Guide For Freshers - Apply for Jobs - Future Studies - Jobs Forums - Freshers IT Software Salary Details