Created
July 21, 2019 18:06
-
-
Save richwednesday/11ee7fb6c077af165906b2efbb20abe3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ************************************************************************** */ | |
/* */ | |
/* ::: :::::::: */ | |
/* sudoku.c :+: :+: :+: */ | |
/* +:+ +:+ +:+ */ | |
/* By: jiwok <[email protected]> +#+ +:+ +#+ */ | |
/* +#+#+#+#+#+ +#+ */ | |
/* Created: 2019/07/20 20:00:06 by jiwok #+# #+# */ | |
/* Updated: 2019/07/20 21:18:15 by jiwok ### ########.fr */ | |
/* */ | |
/* ************************************************************************** */ | |
#include <stdlib.h> | |
#include <unistd.h> | |
void ft_print_sudoku(int **arr) | |
{ | |
int i; | |
int j; | |
i = 0; | |
while (i < 9) | |
{ | |
j = 0; | |
while (j < 9) | |
{ | |
write(1, &arr[i][j], 1); | |
if (j != 8) | |
write(1, " ", 1); | |
j += 1; | |
} | |
write(1, "\n", 1); | |
i += 1; | |
} | |
} | |
int ft_used_in_box(int **arr, int startrow, int startcolumn, int nb) | |
{ | |
int i; | |
int j; | |
startrow = startrow - (startrow % 3); | |
startcolumn = startcolumn - (startcolumn % 3); | |
i = 0; | |
while (i < 3) | |
{ | |
j = 0; | |
while (j < 3) | |
{ | |
if (arr[i + startrow][j + startcolumn] == (nb + 48)) | |
return (1); | |
j += 1; | |
} | |
i += 1; | |
} | |
return (0); | |
} | |
int ft_safe(int **arr, int row, int column, int nb) | |
{ | |
int i; | |
i = 0; | |
while (i < 9) | |
{ | |
if ((arr[row][i] > 48 && arr[row][i] <= 57 && arr[row][i] == (nb + 48)) || | |
(arr[i][column] > 48 && arr[i][column] <= 57 && arr[i][column] == (nb + 48)) || | |
ft_used_in_box(arr, row, column, nb) == 1) | |
return (0); | |
i += 1; | |
} | |
return (1); | |
} | |
int ft_solvesudoku(int **arr) | |
{ | |
int i; | |
int j; | |
int k; | |
i = 0; | |
while (i < 9) | |
{ | |
j = 0; | |
while (j < 9) | |
{ | |
if (arr[i][j] < 49 || arr[i][j] > 57) | |
{ | |
k = 1; | |
while (k <= 9) | |
{ | |
if (ft_safe(arr, i, j, k)) | |
{ | |
arr[i][j] = k + 48; | |
if (ft_solvesudoku(arr) == 1) | |
return (1); | |
arr[i][j] = 48; | |
} | |
k += 1; | |
} | |
return (0); | |
} | |
else | |
j += 1; | |
} | |
i += 1; | |
} | |
return (1); | |
} | |
void ft_assign_elements(int **arr, char **argv) | |
{ | |
int i; | |
int j; | |
i = 0; | |
while (i < 9) | |
{ | |
j = 0; | |
while (j < 9) | |
{ | |
if (argv[i + 1][j] > 48 && argv[i + 1][j] <= 57) | |
arr[i][j] = argv[i + 1][j]; | |
j += 1; | |
} | |
arr[i][j] = '\0'; | |
i += 1; | |
} | |
} | |
int **ft_allocate_memory() | |
{ | |
int **arr; | |
int i; | |
arr = (int **)malloc(sizeof(int *) * 10); | |
i = 0; | |
while (i < 9) | |
{ | |
arr[i] = (int *)malloc(sizeof(int) * 10); | |
i += 1; | |
} | |
arr[i] = 0; | |
return arr; | |
} | |
int main(int argc, char **argv) | |
{ | |
int **arr; | |
if (argc == 10) | |
{ | |
arr = ft_allocate_memory(); | |
ft_assign_elements(arr, argv); | |
if (ft_solvesudoku(arr) == 1) | |
ft_print_sudoku(arr); | |
else | |
write(1, "Error\n", 6); | |
} | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment