/*driver code*/ void main( ) { char puzzle[MAX_SIZE][SIZE]; printf("\n*********************** part1 & part2 **********\n"); int lines=0; fill_puzzle_matrix(puzzle,&lines); printf("Printing the puzzle ...\n"); print_puzzle(puzzle,lines); printf("\n************************** part3 ********************\n"); bool b ; b= is_same_word(puzzle,lines,"LOVE",2,1,'a'); if(b) { printf("there is LOVE starting on puzzle (2,1) across.. \n"); } else printf("LOVE is not found..\n"); printf("\n************************** part4 ********************\n"); if(b) { mark_puzzle(puzzle,lines,2,1,'a',4); print_puzzle(puzzle,lines); } printf("\n************************** part5 ********************\n"); char choose='y'; while(choose == 'y'){ char wordd[SIZE]; int xPoint, yPoint; char dir,space; printf("Enter the word:"); scanf(" %s",wordd); printf("Enter the starting point of the word ,and the direction [across:a, down: d]on puzzle matrix:"); scanf(" %d %d %c",&xPoint,&yPoint,&dir); b= is_same_word(puzzle,lines,wordd,xPoint,yPoint,dir); if(b) { printf("\nThe word is found and replace with '*'character.\n"); mark_puzzle(puzzle,lines,xPoint,yPoint,dir,len_str(wordd)); } printf("The word is not found in puzzle."); print_puzzle(puzzle,lines); printf("\nDo you want to continue ? [yes(y) or no(n)] : "); scanf(" %c",&choose) ; } printf("\n************************** part6_BONUS **************\n"); char word[SIZE]; printf("Enter the word:"); scanf("%s",word); int* par; is_found(puzzle,lines,word,par); }
int main(int ac, char **av) { char cmd_line[MAX_CHARS+1] ; op_result status ; /* * Parse the command arguments. * If this returns, initialize the puzzle. * Then * - configure the board from the command * line puzzle file, * - close the file, * - print the initial board */ parse_args(ac, av) ; init_puzzle() ; configure( puzzle_file() ) ; fclose( puzzle_file() ) ; print_puzzle() ; /* * Command loop. * Read a line and use the first character to decide * what command to execute (or report an error). */ printf("command: "); while(read_line(cmd_line, MAX_CHARS) != EOF ) { if(cmd_line[CMD_INDEX] == 'q') { // quit break ; } else if (cmd_line[CMD_INDEX] == 'p') { // print the board print_puzzle() ; } else if (cmd_line[CMD_INDEX] == 'a') { // add a digit int r, c, d ; r = cmd_line[ROW_INDEX] - '0' ; c = cmd_line[COL_INDEX] - '0' ; d = cmd_line[DIGIT_INDEX] - '0' ; status = add_digit(r, c, d) ; if( status != OP_OK ) { print_error(status) ; } } else if (cmd_line[CMD_INDEX] == 'e') { // erase a digit int r, c ; r = cmd_line[ROW_INDEX] - '0' ; c = cmd_line[COL_INDEX] - '0' ; status = erase_digit(r, c) ; if( status != OP_OK ) { print_error(status) ; } } else { // error printf("Unknown command %s\n", cmd_line) ; } printf("command: ") ; } return 0 ; }
int main(){ // fill in the values, with 0 // for the unknown values // put a 6 at (1, 3) int puzzle[9][9] = { 0,0,6, 0,0,7, 3,0,0, 0,1,8, 0,0,9, 0,5,0, 5,0,0, 0,0,0, 0,6,4, 9,2,0, 0,8,0, 0,0,0, 0,0,0, 7,6,3, 0,0,0, 0,0,0, 0,9,0, 0,7,5, 6,3,0, 0,0,0, 0,0,8, 0,9,0, 3,0,0, 5,2,0, 0,0,2, 4,0,0, 6,0,0 }; /* int puzzle[9][9] = { // 2,9,5, 7,4,3, 8,6,1, 0,9,5, 0,4,3, 8,6,1, 4,3,1, 8,0,5, 9,2,7, 8,7,6, 1,9,2, 5,4,3, 0,8,7, 4,5,9, 2,1,6, 6,1,2, 3,8,7, 4,9,5, 5,4,9, 2,1,6, 7,3,8, 7,6,3, 5,3,4, 1,8,9, 9,2,8, 6,7,1, 3,5,4, 1,5,4, 9,3,8, 6,7,2}; */ // Prints out the initial puzzle print_puzzle(puzzle); printf("%d\n", should_put_in_spot(puzzle, 6, 1, 3)); /* while (!finished(puzzle)){ fill_row_col(puzzle); print_puzzle(puzzle); sleep(3); } */ // Puzzle is solved by this point print_puzzle(puzzle); return EXIT_SUCCESS; }
void bfs(Node *root, Cell *sorted_list){ Queue queue = {}; enqueue(&queue, root, 0); int previous_level = 0; while(!is_empty(&queue) && sorted_list->value != -99){ Node *crt = dequeue(&queue); if(check_puzzle_validity(crt->instance)){ crt->children = assign_children(crt->instance, sorted_list, &crt->number_of_children); int i; #pragma omp parallel num_threads(4) shared(queue) { #pragma omp for for(i=0;i<get_number_of_possibilities(sorted_list);i++){ if(check_puzzle_validity(crt->children[i].instance)==1 && get_number_of_unknowns(crt->children[i].instance) == 0){ printf("FOUND SOLUTION :D\n"); print_puzzle(crt->children[i].instance); return; } // print_puzzle_by_level(crt->children[i].instance, previous_level); enqueue(&queue, &crt->children[i], crt->level+1); } } } if(crt->level != previous_level){ printf("level: %d\n", crt->level); previous_level = crt->level; sorted_list++; } } }
Node *build_tree(Puzzle *instance){ int recheck = 1; while(recheck == 1){ int i; recheck = 0; for(i=0;i<instance->size*instance->size;i++){ if(get_number_of_possibilities(&instance->cells[i]) == 1){ recheck = 1; int value = get_possibility(&instance->cells[i], 0); set_cell_value((&instance->cells[i])->row, (&instance->cells[i])->col, instance, value); } } } printf("Building Tree..\n"); // sort cells in increasing order of possibility list size int *hash = hash_cells(instance); Cell *sorted_list = convert_hash_to_list(hash, instance); print_puzzle(instance); struct timeval start, end; gettimeofday(&start, NULL); Node root = {instance, 0, 0, NULL}; bfs(&root, sorted_list); gettimeofday(&end, NULL); printf("\n\nAlgorithm's computational part duration :%ld\n", \ ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))); }
void solve(unsigned char grid[9][9]) { puzzle_t *puz = create_puzzle(grid); puzzle_t *solved; if ((solved = search(puz)) != NULL) { print_puzzle(solved); } //free_puzzle(puz); }
int main(int argc, char **argv) { int i = 0, j = 0, k = 0; alloc_puzzle(); read_in(); printf("\n\nPuzzle as read in:\n"); print_puzzle(); alloc_cand(); while (!solved()) { if (i == 2) { printf("\nno luck after 20 iterations...\nis the puzzle solvable?\nexiting...\n"); free_mem(); exit(EXIT_SUCCESS); } i++; cand(); printf("\nPuzzle after iteration #%d\n", i); print_puzzle(); /* if (i == 50) { printf("num candidates\n"); for (j = 0; j < N; j++) for (k = 0; k < N; k++) printf("%d_%d: %d\n", j, k, puz_can[j][k][0]); } */ reset(); } /* cand(); printf("\nPuzzle after one candidacy:\n"); print_puzzle(); reset(); cand(); printf("\nPuzzle after two candidacies:\n"); print_puzzle(); reset(); cand(); printf("\nPuzzle after three candidacies:\n"); print_puzzle(); */ return EXIT_SUCCESS; }
void print_solution (puzzle b) { int i; printf ("Here is the solved puzzle:\n"); print_puzzle (b); printf ("Here are the moves needed to solve the puzzle:\n"); for (i = 0; i < b.moves_made; i++) printf ("%d-", b.move[i]); printf ("\n"); }
int main (int argc, char *argv[]) { puzzle best_soln; int best_cost; int i; puzzle s, t, u; int lbsf; initialize_heap(); get_puzzle(&s); startTime = clock()/1000; print_puzzle(s); insert_heap(s); best_cost = 999; lbsf = 0; while(heap_size > 0) { u = delete_heap(); if (u.lower_bound > lbsf) { lbsf = u.lower_bound; } if (u.lower_bound >= best_cost) break; if (solved(u)) { if (u.lower_bound < best_cost) { s = u; best_cost = u.lower_bound; } } else { for (i = 0; i < possible_moves[u.hole]; i++) { t = make_move (u, i); insert_heap (t); } } } print_solution (s); printf("Finished in %i milliseconds.\n", clock()/1000 - startTime); }
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 *) ¤t); pthread_create(&column_t, 0, valid_in_column, (void *) ¤t); pthread_create(&box_t, 0, valid_in_box, (void *) ¤t); // 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; }