-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
67 lines (51 loc) · 1.44 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdbool.h>
#include "board.h"
Board *solve_board(Board *board);
int main(int argc, char *argv[]){
if(argc != 2){
return 1;
}
FILE *fp = fopen(argv[1],"r");
if(fp == NULL){
return 1;
}
Board *board = make_board_from_file(fp);
fclose(fp);
print_board(board);
if(!board_is_valid(board)){
printf("The given board is not valid\n");
destroy_board(board);
return 1;
}
Board *solvedBoard = solve_board(board);
if(solvedBoard == NULL){
printf("The given board is not valid\n");
}
else{
printf("The board was solved!!! :)\n\n");
print_board(solvedBoard);
}
destroy_board(board);
destroy_board(solvedBoard);
return 0;
}
Board *solve_board(Board *board){
if(board_is_filled(board) && board_is_valid(board))
return board;
Cell *minCell = get_cell_min_possibilities(board);
int consideredValue = get_possibility(minCell);
do{
Board *consideredBoard = set_cell(board,minCell,consideredValue);
if(board_is_valid(consideredBoard)){
Board *result = solve_board(consideredBoard);
if(result != NULL){
destroy_board(consideredBoard);
return result;
}
}
destroy_board(consideredBoard);
consideredValue = get_possibility(minCell);
}while (consideredValue != 0);
return NULL;
}