Sie sind auf Seite 1von 1

#include <stdio.

h>
#include <stdlib.h>

int count = 0;
void solve(int n, int col, int *hist)
{
if (col == n) {
printf("\nNo. %d\n-----\n", ++count);
int i;
int j;
for (i = 0; i < n; i++, putchar('\n'))
for (j = 0; j < n; j++)
putchar(j == hist[i] ? 'Q' : '.');

return;
}

//QUEENS/REGINE
//# define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)

//BISHOPS/NEBUNI
//# define attack(i, j) (abs(hist[j] - i) == col - j)

//ROOKS/TURE
# define attack(i, j) (hist[j] == i)
int i;
int j;
for (i = 0, j = 0; i < n; i++) {
for (j = 0; j < col && !attack(i, j); j++);
if (j < col) continue;

hist[col] = i;
solve(n, col + 1, hist);
}
}

int main(int n, char **argv)


{
if (n <= 1 || (n = atoi(argv[1])) <= 0) n = 5;
int hist[n];
solve(n, 0, hist);
}

Das könnte Ihnen auch gefallen