You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.5 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_eight_queens_puzzle2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/08/07 15:47:14 by emartine #+# #+# */
/* Updated: 2017/08/14 15:14:27 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
void ft_putchar(char c);
void sol(int p[8])
{
int i;
i = 0;
while (i < 8)
{
ft_putchar('0' + p[i]);
i++;
}
ft_putchar('\n');
}
int available(int p[8], int x, int y)
{
int i;
i = 1;
while (i < y)
{
if (p[i - 1] == x || p[i - 1] - i == x - y || p[i - 1] + i == x + y)
return (0);
i++;
}
return (1);
}
void recur(int p[8], int x, int y)
{
int nx;
if (y == 8)
{
p[7] = x;
sol(p);
return ;
}
p[y - 1] = x;
nx = 1;
while (nx <= 8)
{
if (nx != x && available(p, nx, y + 1))
recur(p, nx, y + 1);
nx++;
}
}
void ft_eight_queens_puzzle_2(void)
{
int p[8];
int x;
x = 1;
while (x <= 8)
{
recur(p, x, 1);
x++;
}
}