Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Header...
- #ifndef FT_HEADER_H
- # define FT_HEADER_H
- int ft_check_column(char **sudoku, int x, char nr);
- int ft_check_row(char **sudoku, int, char);
- int ft_check_square(char **sudoku, int x, int y, char nr);
- int ft_solve_sudoku(char **sudoku, int, int);
- int ft_check_collision(char **sudoku, int x, int y, char nr);
- void ft_display_sudoku(char **sudoku);
- #endif
- Functie 1, ft_check_row
- #include "ft_header.h"
- int ft_check_row(char **sudoku, int y, char nr)
- {
- int rowc;
- rowc = 0;
- while (rowc < 9)
- {
- if (sudoku[y][rowc] == nr)
- return (1);
- rowc += 1;
- }
- return (0);
- }
- functie 2, ft_check_column
- #include "ft_header.h"
- int ft_check_column(char **sudoku, int x, char nr)
- {
- int colc;
- colc = 0;
- while (colc < 9)
- {
- if (sudoku[colc][x] == nr)
- return (1);
- colc += 1;
- }
- return (0);
- }
- Functie 3; ft_check_square
- #include "ft_header.h"
- int ft_check_square(char **sudoku, int x, int y, char nr)
- {
- int w;
- int l;
- x = x - x % 3;
- y = y - y % 3;
- w = x;
- l = y;
- while (l < (x + 3))
- {
- while (w < (y + 3))
- {
- if (sudoku[l][w] == nr)
- return (1);
- w += 1;
- }
- l += 1;
- }
- return (0);
- }
- Functie 4; ft_solve_sudoku
- #include <unistd.h>
- #include "ft_header.h"
- int ft_solve_sudoku(char **sudoku, int x, int y)
- {
- char nr;
- int tx;
- int ty;
- tx = 0;
- ty = 0;
- nr = '1';
- if ((sudoku[y][x] != '0') && (x == 8 && y == 8))
- return (1);
- if (x <= 8)
- x += 1;
- if (y == 9)
- {
- x = 0;
- y += 1;
- }
- if (ft_solve_sudoku(sudoku, x, y))
- return (1);
- if (sudoku[y][x] == '0')
- {
- while (nr <= '9')
- {
- if (ft_check_collision(sudoku, x, y, nr) != 1)
- {
- sudoku[y][x] = nr;
- if (x == 8 && y == 8)
- return (1);
- if (x <= 8)
- tx = x + 1;
- else if (y <= 8)
- {
- tx = 0;
- ty = y + 1;
- }
- if (ft_solve_sudoku(sudoku, tx, ty))
- return (1);
- }
- nr += 1;
- }
- sudoku[y][x] = '0';
- }
- return (0);
- }
- void ft_display_sudoku(char **sudoku)
- {
- int l;
- int w;
- l = 0;
- w = 0;
- while (l < 9)
- {
- while (w < 9)
- {
- write(1, &sudoku[l][w], 1);
- w += 1;
- }
- l += 1;
- }
- }
- Functie 5; main
- #include "ft_header.h"
- #include <stdlib.h>
- #include <unistd.h>
- int ft_strlen(char *str)
- {
- int i;
- i = 0;
- while (str[i] != '\0')
- i++;
- return (i);
- }
- char **make_valid(char **str)
- {
- int x;
- int y;
- char **sudoku;
- sudoku = (char **)malloc(sizeof(char *) * 9 + 1);
- y = 0;
- while (y < 9)
- {
- if (ft_strlen(str[y]) != 9)
- return (0);
- sudoku[y] = (char *)malloc(sizeof(char) * 9 + 1);
- x = 0;
- while (x < 9)
- {
- if (str[y][x] == '.')
- sudoku[y][x] = 0;
- else if (str[y][x] >= '1' && str[y][x] <= '9')
- sudoku[y][x] = str[y][x] - '0';
- else
- return (0);
- x++;
- }
- y++;
- }
- return (sudoku);
- }
- int main(int arg, char **str)
- {
- char **nrs;
- if (arg != 10)
- {
- write(1, "Error\n", 6);
- return (0);
- }
- nrs = make_valid(&str[1]);
- ft_solve_sudoku(nrs, 0, 0);
- if (nrs == 0)
- write(1, "Error\n", 6);
- ft_display_sudoku(nrs);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement