Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C 9x9 sudoku solver

Name: Anonymous 2011-05-23 10:58

Hey /prog/
could you guys explain how do the "safe" and "solve" functions work in this code? http://pastebin.com/eYQYdUB7
or just add comments next to each line? Anything is fine, really.

Name: Anonymous 2011-05-23 11:14

>>1
safe checks if n is already somewhere vertically, horizontally and diagonally.

solve just bruteforces the sudoku. Iterates all the numbers from 0 to 9 in the current row/cell until it finds a safe cell to put the number, it stores the number there, and moves to the next cell.

Name: Anonymous 2011-05-23 12:11

why is there a return x; after every if?
if the first if in the function safe is true then it returns TRUE and doesn't do the following if's and for's, right?

Name: Anonymous 2011-05-23 12:39

>>3
Right. Maybe learning the language before doing/reading anything will help. That said, that code sucks for almost all the definitions of to suck. Read SICP, read K&R if you want to get into C, read the Intel manuals because if you want to learn C is just to do low-level stuff, and if you really want to do them, C is the wrong language for many low-level tasks, and you should just know how your architecture really works.

Don't change these.
Name: Email:
Entire Thread Thread List