コード例 #1
0
int sudokuHelper(int puzzle[][9], int row, int column) 
{
	if (9 == row) { return 1; }

	if (puzzle[row][column]) 
	{
		if (column == 8) 
		{
			if (sudokuHelper(puzzle, row+1, 0)) return 1;
		} 
		else 
		{
			if (sudokuHelper(puzzle, row, column+1)) return 1;
		}
		return 0;
	}

	for (int nextNumber=1; nextNumber<10; nextNumber++) 
	{
		if(isOk(nextNumber, row, column)) 
		{
			puzzle[row][column] = nextNumber;
			if (column == 8) 
			{
				if (sudokuHelper(puzzle, row+1, 0)) return 1;
			}
			else 
			{
				if (sudokuHelper(puzzle, row, column+1)) return 1;
			}
			puzzle[row][column] = 0;
		}
	}
	return 0;
}
コード例 #2
0
ファイル: sudoku.c プロジェクト: Probotect0r/Sudoku-Solver
int main(int argc, char *argv[]){
  // Read the file puzzle.txt to get the puzzle
  // Store it in an 2D Array
  read_puzzle();

  // Solve the puzzle

  int solved = sudokuHelper(puzzle, 0, 0);
  // Save the solution to solution.txt
  if (solved == 1){
    printf("Puzzle solved!\n");
    save_solution();
  } else {
    printf("Could not find a solution to this puzzle.");
  }

  return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: sudoku.c プロジェクト: Probotect0r/Sudoku-Solver
int sudokuHelper(int puzzle[9][9], int row, int column) {
  print_puzzle();
  printf("Started recursive call. Row: %d\tColumn: %d\n", row, column);
  position current;
  current.row = row;
  current.column = column;
  int val = 1;
  // If all rows have been filled, then return 1
  if (9 == row) {
    return 1;
  }
  // check if cell is empty
  if (puzzle[row][column] != 0) {
    printf("Cell is not Empty!\n");
    // If its the last column go to next row
      if (column == 8) {
          // Call method on next row, first column
          if (sudokuHelper(puzzle, row+1, 0)) return 1;
      } else {
          // Call method on next column
          if (sudokuHelper(puzzle, row, column+1)) return 1;
      }
      return 0;
  }

  // Cell is empty
  // Try all the values until you find one that is valid
  for (; val < 10; val++) {
    printf("Checking for value: %d\n", val);
    current.value = val;
    // Create threads to do the checking at same time
    pthread_t row_t;
    pthread_t column_t;
    pthread_t box_t;

    pthread_create(&row_t, 0, valid_in_row, (void *) &current);
    pthread_create(&column_t, 0, valid_in_column, (void *) &current);
    pthread_create(&box_t, 0, valid_in_box, (void *) &current);

    // Wait for threads to finish checking
    pthread_join(row_t, 0);
    pthread_join(column_t, 0);
    pthread_join(box_t, 0);

    // if the value is valid in current cell
    if (valid_box && valid_row && valid_column) {
      printf("%d was a valid value.\n", val);
      // Save the value to the current cell
      puzzle[row][column] = val;

      // Move on to next cell
      // If 0 is returned, means that a valid value could not be found for the next cell
      // and this cell has to be changed
      if (column == 8) {
          if (sudokuHelper(puzzle, row+1, 0) == 1) return 1;
      } else {
          if (sudokuHelper(puzzle, row, column+1) == 1) return 1;
      }
      // The value we chose was incorrect so reset the cell to 0
      // and continue the loop
      puzzle[row][column] = 0;
    }
  }
  // If none of the values were valid, then one of the previous cells is wrong
  // so we go up a level and continue there
  return 0;
}
コード例 #4
0
int solveSudoku(int puzzle[][9]) 
{
	//init();
	return sudokuHelper(puzzle, 0, 0);
}