Elouan MARTINET 4 years ago
parent
commit
22fdea1137
  1. 47
      ex08/ft_eight_queens_puzzle.c
  2. 72
      ex09/ft_eight_queens_puzzle2.c

47
ex08/ft_eight_queens_puzzle.c

@ -5,23 +5,56 @@
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/08/05 23:39:34 by emartine #+# #+# */
/* Updated: 2017/08/05 23:46:28 by emartine ### ########.fr */
/* Created: 2017/08/04 11:24:25 by emartine #+# #+# */
/* Updated: 2017/08/07 15:46:22 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
int available(int p[8])
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);
}
int recur()
int recur(int p[8], int x, int y)
{
;
int r;
int nx;
if (y == 8)
return (1);
p[y - 1] = x;
nx = 1;
r = 0;
while (nx <= 8)
{
if (nx != x && available(p, nx, y + 1))
r += recur(p, nx, y + 1);
nx++;
}
return (r);
}
// 92
int ft_eight_queens_puzzle(void)
{
int p[8];
int x;
int r;
x = 1;
r = 0;
while (x <= 8)
{
r += recur(p, x, 1);
x++;
}
return (r);
}

72
ex09/ft_eight_queens_puzzle2.c

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_eight_queens_puzzle2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/08/07 15:47:14 by emartine #+# #+# */
/* Updated: 2017/08/07 15:53:41 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
int 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)
{
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_puzzle2(void)
{
int p[8];
int x;
x = 1;
while (x <= 8)
{
recur(p, x, 1);
x++;
}
}
Loading…
Cancel
Save