-3

I'm trying to complete a sudoku solution checker program in c. I'm still trying to understand the steps in building this program before I start coding it. I found this example online http://practicecprogram.blogspot.com/2014/10/c-program-to-find-out-if-solved-sudoku.html

There are a few questions I still don't understand.

1 For my program I am given a text file with the first number being a number that says how many sets of sudoku solutions it contains. I am almost understanding how to check just one solution but having to do it for N solutions and making the program work for multiple sudoku solutions confuses me. Especially in making my 2d arrays for the values. My ouput is supposed to only be Yes or No on a new line for however many N sets.

2 Is checking that all rows and columns have sums of 45 and that the values are >0, <10 enough to prove that the solution is valid? I'm assuming since every puzzle only has one solution I don't have to check each 3x3 grid to make it doesn't contain duplicates if each row and column sum to 45.

tonomon
  • 1
  • 4

3 Answers3

0
  1. Your problem statement is missing an input. The sample assumes that the solution for each game is typed in, but in #1 you state that the only input is the number of games to solve. There has to be a source of data for each game solution. Let's assume there is another file for each game. Your program needs to read in each game solution, verify the solution, and simply report pass or fail as the result. The sample code needs to be re-coded to accept a file-based input but it does not need to retain all solutions in memory at once.
  2. Another rule for Sudoku is that each digit in a row or column may only appear once. Just calculating a total for each row or column won't catch duplicates.
  • 1 Sorry I didn't write that clear enough. I will be reading from only one input file. The first character of the file will be the number of sets of solution sets. Following that number, the text file contains all numbers in the solution sets per however many n sets separated by spaces and in lines of 9. The problem I'm having is understanding how to have the program read sets of 81 numbers without knowing how many n sets there will be. After checking the first 2d array in all ways and printing the results, how do I file scan in the next set of numbers starting at the correct spot? – tonomon Jan 25 '16 at 04:39
  • 2 I'm familiar with that rule but trying to figure out if it is necessary to check for repetition. Wouldn't any 9x9 grid of numbers containing 1-9 and having all columns and rows sum to 45 be a solved sudoku puzzle? How would one test something like that? That was the reasoning I was using when considering if checking for repetition is necessary or not. – tonomon Jan 25 '16 at 04:46
0

1) simple:

/// Read the number of puzzles;
...
for (i = 0; i < number_of_puzzless; i++) {
   // Read Data for a puzzle
   ...
   // Process puzzle data
   ...
   // Print result
   ... 
}

2) The sum or all cells in a row/column is equal to 45 and all numbers are in a range from 1 to 9. Is this enough to check only rows and columns to state that the whole puzzle is valid?

Yes it is. Even if you try to fool your checker and would give it a row that, say, has two sixes and two nines and no sevens and eights, this would break the checks on some columns.

Serge
  • 6,088
  • 17
  • 27
  • The problem I'm having is how to read in the data correctly for puzzles after the first. supposed the input file looks like this 3 (81 values 81 values 81 values). How do I start the second puzzle's array to read from the second set then the third etc? Especially without knowing the N sets this is confusing me 2) Thank you I was wondering about this – tonomon Jan 25 '16 at 04:56
  • From your questions I see that you don't understand how the file IO is conducted in C. I think that you need to go through some tutorial, like this one: http://www.tutorialspoint.com/ansi_c/c_working_with_files.htm – Serge Jan 25 '16 at 05:05
  • I'm mostly familiar with those functions. I'm aware I will need to scan in data from a text file after opening and then store the values in a 2D array. Just not sure how to scan in correctly for any puzzle that isn't the first. – tonomon Jan 25 '16 at 05:26
  • Listen, first you read the number of puzzles from the file. The file pointer is advanced. Then you read 81 numbers of a puzzle one by one. The file pointer is advanced forward each time you read a number from the file. When you are done with reading data of a single puzzle, you analyse it. When you done with analysis and result printout, you continue with a new puzzle to read at current file pointer position. – Serge Jan 25 '16 at 05:35
0

1) Handle each puzzle one at a time. Read the puzzle into the array. Check it. Then read the next puzzle into the same array. If you only want a single yes/no for all the puzzles collectively you can print no and exit as soon as any check fails. If you make it to the end without any failed checks, then print yes. If you need to print whether individual puzzles passed, then answer before moving on to the next puzzle.

2)No! Absolutely not. One simple example is a sudoku filled with all 5s. This will give a 45 sum for every block, row, and column, but obviously is incorrect. It is also not sufficient to only check the presence of each digit along the rows and columns. For example consider one filled with 1 - 9 on the first row. Each successive row is a left rotate of the previous row. This would give all digits in each row and each column, but the blocks would be wrong e.g. block 0,0 would be 123,234,456.

One (maybe the best) way to check for each number is to set up an array of flags. Each index represents that number in the puzzle. Scan the line (or row or block) and set the flag for each number when you reach it. Then check to make sure all the flags are set. For checking a row:

int i,col;
int flags[9];
//zero the flags
for(i 0 1; i < 9; i++)
    flags[i] = 0;

//check row
for(col = 0; col < 9; col++)
    flags[data[row][col] - '1'] = 1;

//check flags
for(i 0 1; i < 9; i++)
    if( 0 == flags[i] )
        fail = true;
James Peters
  • 111
  • 4