diff options
Diffstat (limited to 'solve.c')
-rw-r--r-- | solve.c | 174 |
1 files changed, 174 insertions, 0 deletions
@@ -0,0 +1,174 @@ +#include <stdio.h> +#include <string.h> + +#include "cell.h" +#include "display.h" + +void update_not_row_col(struct cell b[9][9]) +{ + int x = 0; + int y = 0; + int i = 0; + int val = 0; + + for (y = 0; y < 9; y++) + { + for (x = 0; x < 9; x++) + { + val = b[x][y].val; + if (val != 0) + { + for (i = 0; i < 9; i++) + { + b[i][y].not[val - 1] = 1; + b[x][i].not[val - 1] = 1; + } + } + } + } +} + +void not_to_val_row_col(struct cell b[9][9]) +{ + int x = 0; + int y = 0; + int i = 0; + int val = 0; + int total = 0; + + for (y = 0; y < 9; y++) + { + total = 0; + for (x = 0; x < 9; x++) + { + if (b[x][y].val != 0) + continue; + + for (i = 0; i < 9; i++) + { + if (b[x][y].not[i]) + { + total++; + } else { + val = i + 1; + } + } + if (total == 8) + b[x][y].val = val; + } + } +} + + +void not_to_val_cell(struct cell b[9][9]) +{ + int cx = 0; + int cy = 0; + int x = 0; + int y = 0; + int n = 0; + int potentials = 0; + int okx, oky; + struct cell c; + + /* refactor this crap */ + for (cy = 0; cy < 9; cy += 3) + { + for (cx = 0; cx < 9; cx += 3) + { + /* for each val that has to be in cell */ + for (n = 1; n <= 9; n++) + { + for (y = 0; y < 3; y++) + { + for (x = 0; x < 3; x++) + { + c = b[cx+x][cy+y]; + if (c.val == n) + { + potentials = 0; + y = 3; + break; + } + if (c.val) + continue; + + if (c.not[n-1] == 0) + { + potentials++; + okx = x; + oky = y; + } + } + } + if (potentials == 1) + { + b[cx+okx][cy+oky].val = n; + potentials = 0; + } + } + } + } +} + +int main(int argc, char **argv) +{ +// int x = 0; +// int y = 0; + struct cell board[9][9]; + + memset(board, 0, sizeof(board)); + + + board[7][0].val = 3; + board[4][1].val = 3; + board[0][7].val = 3; + board[1][4].val = 3; + + /* dummy board taken from the guardian lol */ + /* FIXME: have a way to input boards, silly */ + board[0][0].val = 7; + board[0][4].val = 5; + + board[1][2].val = 5; + board[1][4].val = 9; + + board[2][1].val = 6; + board[2][2].val = 4; + board[2][5].val = 2; + + board[3][0].val = 1; + board[3][5].val = 9; + board[3][6].val = 7; + + board[4][0].val = 9; + board[4][2].val = 6; + board[4][3].val = 4; + board[4][7].val = 8; + board[4][8].val = 5; + + board[5][4].val = 2; + + board[6][4].val = 8; + board[6][6].val = 5; + board[6][7].val = 6; + + board[7][1].val = 8; + board[7][6].val = 3; + + board[8][0].val = 4; + board[8][4].val = 1; + board[8][5].val = 7; + board[8][8].val = 8; + + + + display(board); + + update_not_row_col(board); + not_to_val_cell(board); + not_to_val_row_col(board); + + display(board); + +} |