Пример #1
0
short sudoku_solve(Sudoku *sudoku, const short i) {
  short n;  
  short * arr_of_nine;
  if (i == FIELD_SIZE) {
    return TRUE; // end reached
  } else if (sudoku->grid[i] == 0) {  
    arr_of_nine = (short *)malloc(sizeof(short) * ARC_SIZE);
    sudoku_possible(sudoku, arr_of_nine, sudoku_coord(i));
    for (n = 0; n < ARC_SIZE; ++n) {
      if (arr_of_nine[n] == 0) continue;
      sudoku->grid[i] = arr_of_nine[n];
       // sudoku_print(&sudoku);
       // printf("%s\033[1;1H");
      if (sudoku_solve(sudoku, i + 1) == TRUE) {
        free(arr_of_nine);
        return TRUE;
      }
    }
    sudoku->grid[i] = 0;
    free(arr_of_nine);
    return FALSE;
  } else {
    return sudoku_solve(sudoku, i + 1);
  }
}
Пример #2
0
int main(int argc, char *argv[])
{
    int     c;
    size_t  n;
    char    puzzle[82];
    char    solution[82];

    while ((c = getopt(argc, argv, optstring)) != -1) {
        switch (c) {
            case 'c':
                g_count = atoi(optarg);
                break;
            case 'v':
                g_verbose_flag = 1;
                break;
            case '?':
                usage(argc, argv);
                exit(EXIT_FAILURE);
                break;
        }
    }

    for (c = 0; c < 82; c++)    /* just to be safe when calling strlen */
        puzzle[c] = '\0';

    /* read 81 char, else error exit */
    while ((n = strlen(puzzle)) < 81) {
        if (NULL == fgets(puzzle + n, sizeof(puzzle) - n, stdin))
            break;
    }
    if ((n = strlen(puzzle)) < 81) {
        if (g_verbose_flag)
            fprintf(stderr,
                    "Error: not enough characters for a full 9x9 puzzle\n");
        exit(EXIT_FAILURE);
    }

    /* read successful, now process puzzle */
    if (g_count > 0) {
        n = sudoku_nsolve(puzzle, solution, g_count);
        if (g_verbose_flag)
            fprintf(stderr, "%lu\n", (unsigned long) n);
        if (n > 0)
            printf("%s\n", solution);
        exit(2);
    } else {
        if (sudoku_solve(puzzle, solution)) {
            printf("%s\n", solution);
            exit(EXIT_SUCCESS);
        } else {
            if (g_verbose_flag)
                fprintf(stderr, "No solution found.\n");
            exit(EXIT_FAILURE);
        }
    }
    return 0;
}
Пример #3
0
/*
 * Domain Function:  test
 */
void
sudoku_test( void )
{
  /* ::setz1_given(  ) */
  sudoku_setz1_given();
  /* ::solve(  ) */
  sudoku_solve();
  /* ::cleanup(  ) */
  sudoku_cleanup();

}
Пример #4
0
static PRESULT sudoku_solve_callback(POBJECT_HEAD obj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    UINT8   unact;

    switch (event)
    {
    case EVN_UNKNOWN_ACTION:
        unact = (VACTION)(param1>>16);
        if (unact == VACT_ENTER)
        {
            sudoku_solve();
        }
        break;
    }

    return ret;
}
Пример #5
0
int main (int argc, char const *argv[])
{
  int i;
  Sudoku sudoku;
  
  if (argc == 1) {
    printf("usage: %s <file1> <filen> ...", argv[i]);
    exit(1);
  }
  
  for (i = 1; i < argc; ++i) {
    sudoku_initialize(&sudoku, argv[i]);
    sudoku_solve(&sudoku, 0);
    printf("file %s:\n", argv[i]);
    sudoku_print(&sudoku);
  }
  
  return 0;
}
Пример #6
0
int main(void)
{
  int n, m, i, j;
  int **A;
  ecp *p;

  /* load problem */
  printf("\nProblem\n");
  sudoku *s = sudoku_new(stdin);
  sudoku_show_problem(stdout, s);

  /* solve */
  printf("\nAnswer\n");
  sudoku_solve(s);
  sudoku_show_answer(stdout, s);

  /* free */
  sudoku_free(s);

  return 0;
}