Thread: Sudoku

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    13

    Sudoku

    Hello to everyone . I have a coursework in C that i have to do a sudoku solver in C that reads a problem file which is .txt and inside there is the sudoku that must be solved. And then the code solves that and creates another file the solution file also in .txt containing. Also it should provide the exit code 0 if it is succesful if not the code should exit in a non-zero value. Anyone that can helps?????

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sudoku puzzles are usually read in as a string of 81 chars or int's. Using redirection for input, is handy and most common:
    Code:
    for(i=0;i<81;i++)
       fscanf(stdin, "%d", &grid[i]);
    Using explicit filenames is not as common, but just change stdin to the name of your file pointer, and you're in business. Of course, you have to first open a file with fopen, unless it's stdin. C opens that file for you.

    What you want to program is a brute force solver, of course. There are MANY esoteric ways to solve a Sudoku puzzle - you would need a HUGE amount of time to learn and program half of them, so a brute force solver it is.

    Most puzzles are solved by using a technique called naked singles/forced singles, which fits right into the one law of Sudoku:
    "Only one number on each row or column, or box". (boxes are 9 squares, there are 6 of them, one in each section of the puzzle grid).

    Here's a hundred puzzle grids that are guaranteed to have just one solution (this is a real problem in Sudoku. Some puzzles have tens of thousands of solutions, which makes checking the answer to see if it's right, a real problem). Most fans of the puzzle don't accept grids with more than one solution, as valid puzzles.

    Code:
    000000010400000000020000000000050407008000300001090000300400200050100000000806000
    000000010400000000020000000000050604008000300001090000300400200050100000000807000
    000000012000035000000600070700000300000400800100000000000120000080000040050000600
    000000012003600000000007000410020000000500300700000600280000040000300500000000000
    000000012008030000000000040120500000000004700060000000507000300000620000000100000
    000000012040050000000009000070600400000100000000000050000087500601000300200000000
    000000012050400000000000030700600400001000000000080000920000800000510700000003000
    000000012300000060000040000900000500000001070020000000000350400001400800060000000
    000000012400090000000000050070200000600000400000108000018000000000030700502000000
    000000012500008000000700000600120000700000450000030000030000800000500700020000000
    000000012700060000000000050080200000600000400000109000019000000000030800502000000
    000000012800040000000000060090200000700000400000501000015000000000030900602000000
    000000012980000000000600000100700080402000000000300600070000300050040000000010000
    000000013000030080070000000000206000030000900000010000600500204000400700100000000
    000000013000200000000000080000760200008000400010000000200000750600340000000008000
    000000013000500070000802000000400900107000000000000200890000050040000600000010000
    000000013000700060000508000000400800106000000000000200740000050020000400000010000
    000000013000700060000509000000400900106000000000000200740000050080000400000010000
    000000013000800070000502000000400900107000000000000200890000050040000600000010000
    000000013020500000000000000103000070000802000004000000000340500670000200000010000
    000000013040000080200060000609000400000800000000300000030100500000040706000000000
    000000013040000080200060000906000400000800000000300000030100500000040706000000000
    000000013040000090200070000607000400000300000000900000030100500000060807000000000
    000000013040000090200070000706000400000300000000900000030100500000060807000000000
    000000013200800000300000070000200600001000000040000000000401500680000200000070000
    000000013400200000600000000000460500010000007200500000000031000000000420080000000
    000000013400800000200000070000400900001000000060000000000501600380000200000070000
    000000014000000203800050000000207000031000000000000650600000700000140000000300000
    000000014000020000500000000010804000700000500000100000000050730004200000030000600
    000000014000708000000000000104005000000200830600000000500040000030000700000090001
    000000014008005000020000000000020705100000000000000800070000530600140000000200000
    000000014008005000020000000000020805100000000000000700070000530600140000000200000
    000000014008009000020000000000020805100000000000000700070000930600140000000200000
    000000014700000000000500000090014000050000720000600000000900805600000900100000000
    000000014790000000000200000000003605001000000000000200060000730200140000000800000
    000000014970000000000200000000003605001000000000000200060000730200140000000800000
    000000015000400070300060000800000200000104000400500000000023600010000000070000000
    000000015000400070400000000609000300000100800000700000500030200000060040010000000
    000000015000800070300000000408000300000100400000700000500040200000090060010000000
    000000015000800070400000000609000300000100800000700000500030200000060040010000000
    000000015000830000000000200023000800000001000080000000105040000000600720900000000
    000000015000830000000000200026000800000001000080000000105040000000300720900000000
    000000015000900070400000000608000300000100800000700000500030200000060040010000000
    000000015000900070400000000609000300000100800000700000500030200000060040010000000
    000000015000900080300000000704000300000100400000800000500040200000070060010000000
    000000015000900080400000000704000300000100900000800000500030200000070060010000000
    000000015020060000000000408003000900000100000000008000150400000000070300800000060
    000000015040080000000000300000040260500107000900000000300500000080000400000900000
    000000015300600000000000080600050200000001000000000040010200700000760300008000000
    000000015790000000000200000000008706001000000000000900070000830400150000000300000
    000000016000500040300070000900000200000408000700600000000023700040000000010000000
    000000016000708000000000050501200000300000800600000000040000200000053000080010000
    000000016000900080500000000405000300000100500000800000600040200000030070010000000
    000000016040005000000020000000600430200010000300000500000003700100800000002000000
    000000016070000040050200000400060300000005200000041000000900780100000000000000000
    000000016070000040050200000400060300000008200000041000000500790100000000000000000
    000000016200000000000300000601700002000900500400000000030000800000060040050040000
    000000016200080000009000000000420500010000000000000200000106030500000780000900000
    000000017090600000000000030400500200001000000000080000720000600000410500000003000
    000000017090600000000000050200000803000050400000001000600200300041070000000000000
    000000017090800000000000040007060300050000200000001000600300800401000000000050000
    000000017300080000000000000007100006000040300085000000200000840010700000000500000
    000000017600020000000000000153000000000080200007000000400301500020000600000700000
    000000018020500000000000000040000700600000500000041000000700260108300000400000000
    000000018050600000000000030400500200001000000000090000820000600000410700000003000
    000000018200400000000000070000008003000500200010000000502000600000040300000017000
    000000018320000000400000000008051000040000300000070000706000090000300700000200000
    000000018700040000000000030420000700000001000000300000500070200601800000040000000
    000000019000250000000000000091000030000400700030000000400000208200060500000001000
    000000019030050000000000020109000000000400700000870000000102000060000800500000300
    000000019030050000000000020109000000000400800000870000000102000060000700500000300
    000000019070000030200800000050600200001000000000200000000019500600000400000030000
    000000019300600000000000000600080500040000300000010000480000070000200400010900000
    000000019500600000000000000600080500040000300000010000380000040000200700010900000
    000000019500600000000000000600080500040000300000010000480000070000200400010900000
    000000019500800000000000000300070500040000300000010000470000060000200400010900000
    000000019800500000000000000300070500040000300000010000470000060000200400010900000
    000000021000030070040080000100207000050000400000000003200100000000040500000600000
    000000021000083000000040000500200070080000400030900000000060800100500000200000000
    000000021000083000000040000500200070080000400030900000000060800100700000200000000
    000000021000306000000800000400010600000700300200000000000090040530000000086000000
    000000021000407000000008000031060000000000750020000000500210000400000800000300000
    000000021000500030400600000000021000800000007500000600000400800010070000030000000
    000000021004090000070000030100203000500800000006000000200000600000060400030000000
    000000021005080000600000000000670300120000500400000000000201040003000000080000000
    000000021006800000000000070070021000020000400000005000500430600100000000000600000
    000000021030400000700000000100082000000000540000000000000560300290000000004700000
    000000021030600000000080000201000050500400000000370000700002000080000300000000600
    000000021040500000700000000100082000000000650000000000000610400320000000005700000
    000000021040500000800000000700092000000000650000000000000610400320000000005800000
    000000021040600000000000000201000050500800000000400300700020000060000800000300400
    000000021050030000000800000102000070700300000000540000600002000030000400000000500
    000000021060300000000708000100050040070000300000020000200040000000600800500000000
    000000021060500000000090000400002000070000300000600000102400000000030640800000000
    000000021060700000000000000402000000000600300500000700000340050080000600100002000
    000000021070030000000040000100205040030000800000100000200600000000070300600000000
    000000021070030000000090000100205040030000800000100000200600000000070300600000000
    000000021070300000000000000402000000000700300600000800000540060090000500100002000
    000000021070300000000408000100060050030000400000020000200050000000700800600000000
    000000021080300000000409000100060050030000400000020000200070000000800900500000000
    Zeroes or a dot . mean that square is empty.

    A console grid should look like this:
    Code:
    +-------+-------+-------+
    | 1 2 3 | 4 5 6 | 7 8 9 |
    | 7 8 9 | 1 2 3 | 4 5 6 |
    | 4 5 6 | 7 8 9 | 1 2 3 |
    +-------+-------+-------+
    | . . . | . . . | . . . |
    | . . . | . . . | . . . |
    | . . . | . . . | . . . |
    +-------+-------+-------+
    | . . . | . . . | . . . |
    | . . . | . . . | . . . |
    | . . . | . . . | . . . |
    +-------+-------+-------+
    Programming Sudoku is all about doing as little as possible. Think twice before you add anything to your inner loops. That can really kill your programs speed if it's not done with utmost care.

    Get started on getting input into your program. I like having a set file to test, rather than using stdin while I'm coding. That way I can run it from the IDE, without going to a command (terminal) window.

    When you get the basics working, I'll send you over to the Sudoku programming experts. Two things you should read:

    1) How to solve Sudoku: Naked Singles/Forced Singles
    The rest of the how to solve info, you won't need. There are a zillion sites for this. I liked the Times of London webpage for it, but they have gone subscription I am told. There are zillions of others, however.

    2) Backtracking, on Wikipedia. Your program will need it.

    Be sure you understand the One law of Sudoku.

    Post back when you're ready.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Code:
    000000010400000000020000000000050407008000300001090000300400200050100000000806000
    000000010400000000020000000000050604008000300001090000300400200050100000000807000
    000000012000035000000600070700000300000400800100000000000120000080000040050000600
    000000012003600000000007000410020000000500300700000600280000040000300500000000000
    000000012008030000000000040120500000000004700060000000507000300000620000000100000
    000000012040050000000009000070600400000100000000000050000087500601000300200000000
    000000012050400000000000030700600400001000000000080000920000800000510700000003000
    000000012300000060000040000900000500000001070020000000000350400001400800060000000
    000000012400090000000000050070200000600000400000108000018000000000030700502000000
    000000012500008000000700000600120000700000450000030000030000800000500700020000000
    000000012700060000000000050080200000600000400000109000019000000000030800502000000
    000000012800040000000000060090200000700000400000501000015000000000030900602000000
    000000012980000000000600000100700080402000000000300600070000300050040000000010000
    000000013000030080070000000000206000030000900000010000600500204000400700100000000
    000000013000200000000000080000760200008000400010000000200000750600340000000008000
    000000013000500070000802000000400900107000000000000200890000050040000600000010000
    000000013000700060000508000000400800106000000000000200740000050020000400000010000
    000000013000700060000509000000400900106000000000000200740000050080000400000010000
    000000013000800070000502000000400900107000000000000200890000050040000600000010000
    000000013020500000000000000103000070000802000004000000000340500670000200000010000
    000000013040000080200060000609000400000800000000300000030100500000040706000000000
    000000013040000080200060000906000400000800000000300000030100500000040706000000000
    000000013040000090200070000607000400000300000000900000030100500000060807000000000
    000000013040000090200070000706000400000300000000900000030100500000060807000000000
    000000013200800000300000070000200600001000000040000000000401500680000200000070000
    000000013400200000600000000000460500010000007200500000000031000000000420080000000
    000000013400800000200000070000400900001000000060000000000501600380000200000070000
    000000014000000203800050000000207000031000000000000650600000700000140000000300000
    000000014000020000500000000010804000700000500000100000000050730004200000030000600
    000000014000708000000000000104005000000200830600000000500040000030000700000090001
    000000014008005000020000000000020705100000000000000800070000530600140000000200000
    000000014008005000020000000000020805100000000000000700070000530600140000000200000
    000000014008009000020000000000020805100000000000000700070000930600140000000200000
    000000014700000000000500000090014000050000720000600000000900805600000900100000000
    000000014790000000000200000000003605001000000000000200060000730200140000000800000
    000000014970000000000200000000003605001000000000000200060000730200140000000800000
    000000015000400070300060000800000200000104000400500000000023600010000000070000000
    000000015000400070400000000609000300000100800000700000500030200000060040010000000
    000000015000800070300000000408000300000100400000700000500040200000090060010000000
    000000015000800070400000000609000300000100800000700000500030200000060040010000000
    000000015000830000000000200023000800000001000080000000105040000000600720900000000
    000000015000830000000000200026000800000001000080000000105040000000300720900000000
    000000015000900070400000000608000300000100800000700000500030200000060040010000000
    000000015000900070400000000609000300000100800000700000500030200000060040010000000
    000000015000900080300000000704000300000100400000800000500040200000070060010000000
    000000015000900080400000000704000300000100900000800000500030200000070060010000000
    000000015020060000000000408003000900000100000000008000150400000000070300800000060
    000000015040080000000000300000040260500107000900000000300500000080000400000900000
    000000015300600000000000080600050200000001000000000040010200700000760300008000000
    000000015790000000000200000000008706001000000000000900070000830400150000000300000
    000000016000500040300070000900000200000408000700600000000023700040000000010000000
    000000016000708000000000050501200000300000800600000000040000200000053000080010000
    000000016000900080500000000405000300000100500000800000600040200000030070010000000
    000000016040005000000020000000600430200010000300000500000003700100800000002000000
    000000016070000040050200000400060300000005200000041000000900780100000000000000000
    000000016070000040050200000400060300000008200000041000000500790100000000000000000
    000000016200000000000300000601700002000900500400000000030000800000060040050040000
    000000016200080000009000000000420500010000000000000200000106030500000780000900000
    000000017090600000000000030400500200001000000000080000720000600000410500000003000
    000000017090600000000000050200000803000050400000001000600200300041070000000000000
    000000017090800000000000040007060300050000200000001000600300800401000000000050000
    000000017300080000000000000007100006000040300085000000200000840010700000000500000
    000000017600020000000000000153000000000080200007000000400301500020000600000700000
    000000018020500000000000000040000700600000500000041000000700260108300000400000000
    000000018050600000000000030400500200001000000000090000820000600000410700000003000
    000000018200400000000000070000008003000500200010000000502000600000040300000017000
    000000018320000000400000000008051000040000300000070000706000090000300700000200000
    000000018700040000000000030420000700000001000000300000500070200601800000040000000
    000000019000250000000000000091000030000400700030000000400000208200060500000001000
    000000019030050000000000020109000000000400700000870000000102000060000800500000300
    000000019030050000000000020109000000000400800000870000000102000060000700500000300
    000000019070000030200800000050600200001000000000200000000019500600000400000030000
    000000019300600000000000000600080500040000300000010000480000070000200400010900000
    000000019500600000000000000600080500040000300000010000380000040000200700010900000
    000000019500600000000000000600080500040000300000010000480000070000200400010900000
    000000019500800000000000000300070500040000300000010000470000060000200400010900000
    000000019800500000000000000300070500040000300000010000470000060000200400010900000
    000000021000030070040080000100207000050000400000000003200100000000040500000600000
    000000021000083000000040000500200070080000400030900000000060800100500000200000000
    000000021000083000000040000500200070080000400030900000000060800100700000200000000
    000000021000306000000800000400010600000700300200000000000090040530000000086000000
    000000021000407000000008000031060000000000750020000000500210000400000800000300000
    000000021000500030400600000000021000800000007500000600000400800010070000030000000
    000000021004090000070000030100203000500800000006000000200000600000060400030000000
    000000021005080000600000000000670300120000500400000000000201040003000000080000000
    000000021006800000000000070070021000020000400000005000500430600100000000000600000
    000000021030400000700000000100082000000000540000000000000560300290000000004700000
    000000021030600000000080000201000050500400000000370000700002000080000300000000600
    000000021040500000700000000100082000000000650000000000000610400320000000005700000
    000000021040500000800000000700092000000000650000000000000610400320000000005800000
    000000021040600000000000000201000050500800000000400300700020000060000800000300400
    000000021050030000000800000102000070700300000000540000600002000030000400000000500
    000000021060300000000708000100050040070000300000020000200040000000600800500000000
    000000021060500000000090000400002000070000300000600000102400000000030640800000000
    000000021060700000000000000402000000000600300500000700000340050080000600100002000
    000000021070030000000040000100205040030000800000100000200600000000070300600000000
    000000021070030000000090000100205040030000800000100000200600000000070300600000000
    000000021070300000000000000402000000000700300600000800000540060090000500100002000
    000000021070300000000408000100060050030000400000020000200050000000700800600000000
    000000021080300000000409000100060050030000400000020000200070000000800900500000000
    Y'know it's strange... I never could see the hidden picture...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Quote Originally Posted by CommonTater View Post
    Y'know it's strange... I never could see the hidden picture...
    I guess his phone number is in it, so you can ask him...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Thank you very much adak! I will do my research and i ll contact you soon!!

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    adak how can i contact you else?

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    adak can i sent you to ur email what i ve done so far?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku
    By code_lover in forum C Programming
    Replies: 9
    Last Post: 09-16-2011, 11:19 AM
  2. Like Sudoku, but not.
    By quzah in forum Game Programming
    Replies: 4
    Last Post: 08-27-2011, 05:33 AM
  3. sudoku
    By ElemenT.usha in forum C++ Programming
    Replies: 22
    Last Post: 02-14-2011, 10:46 PM
  4. Sudoku
    By mrusnak in forum C Programming
    Replies: 10
    Last Post: 03-20-2009, 06:31 AM
  5. sudoku help
    By blue4life20 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2005, 03:50 PM

Tags for this Thread