Added cleaner resolution for level-2 n_queens
This commit is contained in:
parent
94ff9315d7
commit
eb777e722d
@ -1,93 +1,68 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void ft_putchar(char c)
|
||||
int *board;
|
||||
int target;
|
||||
|
||||
int ft_abs(int value)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
if (value < 0)
|
||||
return (value * -1);
|
||||
return (value);
|
||||
}
|
||||
|
||||
void ft_putnbr(int n)
|
||||
void print_board()
|
||||
{
|
||||
if (n >= 10)
|
||||
ft_putnbr(n / 10);
|
||||
ft_putchar((n % 10) + '0');
|
||||
int i = 0;
|
||||
while (i < target)
|
||||
{
|
||||
if (i == 0)
|
||||
fprintf(stdout, "%i", board[i]);
|
||||
else
|
||||
fprintf(stdout, " %i", board[i]);
|
||||
i++;
|
||||
}
|
||||
fprintf(stdout,"\n");
|
||||
}
|
||||
|
||||
void print_solution(int *board, int n)
|
||||
int is_safe(int row, int col)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
ft_putnbr(board[i]);
|
||||
if (i < n - 1)
|
||||
ft_putchar(' ');
|
||||
i++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
int c = 0;
|
||||
while (c < col)
|
||||
{
|
||||
if (board[c] == row)
|
||||
return (0);
|
||||
if (ft_abs(board[c] - row) == ft_abs(col -c))
|
||||
return (0);
|
||||
c++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_abs(int nbr)
|
||||
void solve(int col)
|
||||
{
|
||||
if (nbr < 0)
|
||||
return (nbr * -1);
|
||||
else
|
||||
return (nbr);
|
||||
if (col == target)
|
||||
return (print_board());
|
||||
|
||||
int row = 0;
|
||||
while (row < target)
|
||||
{
|
||||
if (is_safe(row, col))
|
||||
{
|
||||
board[col] = row;
|
||||
solve(col + 1);
|
||||
}
|
||||
row++;
|
||||
}
|
||||
}
|
||||
|
||||
int is_safe(int *board, int n, int row, int col)
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
(void)n;
|
||||
while (i < col)
|
||||
{
|
||||
if (board[i] == row)
|
||||
return (0);
|
||||
if (ft_abs(board[i] - row) == (col - i))
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void solve(int *board, int n, int col)
|
||||
{
|
||||
if (col == n)
|
||||
return(print_solution(board, n));
|
||||
|
||||
int row = 0;
|
||||
while (row < n)
|
||||
{
|
||||
if (is_safe(board, n, row, col))
|
||||
{
|
||||
board[col] = row;
|
||||
solve(board, n, col + 1);
|
||||
}
|
||||
row++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int *board;
|
||||
int n;
|
||||
|
||||
if (argc != 2)
|
||||
return (fprintf(stderr, "Usage: %s n\n", argv[0]), 1);
|
||||
|
||||
n = atoi(argv[1]);
|
||||
if (n <= 0)
|
||||
return (0);
|
||||
|
||||
board = (int *)malloc(sizeof(int) * n);
|
||||
if (!board)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
return (1);
|
||||
}
|
||||
solve(board, n, 0);
|
||||
free(board);
|
||||
return (0);
|
||||
if (ac != 2)
|
||||
return (1);
|
||||
target = atoi(av[1]);
|
||||
board = (int *)calloc(target, sizeof(int));
|
||||
solve(0);
|
||||
free(board);
|
||||
return (0);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user