FRESHERSHOME
Freshers Forum

C program help with sudoku puzzle routine to solve?

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 ...


Go Back   Jobs in India Forum > Freshers Zone > Puzzles

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

 
Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2007, 11:59 PM
YahooHoo
 
Join Date: Dec 2007
Posts: 1
Default C program help with sudoku puzzle routine to solve?

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
sudoku

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

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


All times are GMT +6.5. The time now is 11:01 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
FreshersHome.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62