请问怎样出结果(N皇后问题)
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static inline int conflict(int *stack, int i, int j)
{
int k;
for (k = 0; k < i; k++)
{
if (j == stack[k] || abs(i - k) == abs(j - stack[k]))
{
return 1;
}
}
return 0;
}
static inline void push(int *stack, int row, int col)
{
stack[row] = col;
}
static inline int pop(int *stack, int row)
{
int col = stack[row];
stack[row] = -1;
return col;
}
static inline int top(int *stack, int n)
{
int row;
for (row = n - 1; row >= 0; row--)
{
if (stack[row] != -1)
{
return row;
}
}
return 0;
}
static char **solution(int *stack, int n)
{
int row, col;
char **solution = (char **)malloc(n*sizeof(char *));
for (row = 0; row < n; row++)
{
char *line = (char *)malloc(n + 1);
for (col = 0; col < n; col++)
{
line[col] = col == stack[row] ? 'Q' : '.';
}
line[n] = '\0';
solution[row] = line;
}
return solution;
}
char*** solveNQueens(int n, int *returnSize)
{
int row = 0, col = 0, sum = 0;
char ***solutions = (char ***)malloc(1000 * sizeof(char **));
int *stack = (int *)malloc(n * sizeof(int));
for (row = 0; row < n; row++)
{
stack[row] = -1;
}
if (n == 1)
{
stack[0] = 0;
solutions[0] = solution(stack, n);
*returnSize = 1;
return solutions;
}
for (; ;)
{
for (; row < n; row++)
{
while (col < n)
{
if (conflict(stack, row, col))
{
while (col == n - 1)
{
if (--row < 0)
{
free(stack);
*returnSize = sum;
return solutions;
}
col = pop(stack, row);
}
col++;
} else {
push(stack, row, col);
break;
}
}
col = 0;
}
row = top(stack, n);
if (row == n - 1) {
solutions[sum++] = solution(stack, n);
}
col = pop(stack, row);
col++;
}
assert(0);
}
int main(int argc, char **argv)
{
int i, n, row, col, num_of_solution;
if (argc != 2)
{
printf("Usage:queen 8");
printf("\n");
exit(-1);
}
n = atoi(argv[1]);
char ***solutions = solveNQueens(n, &num_of_solution);
for(i= 0;i<num_of_solution;i++)
{
char **solution = solutions[i];
for (row = 0; row < n; row++)
{
char *line = solution[row];
for (col = 0; col < n; col++)
{
putchar(line[col]);
}
putchar(' ');
}
printf("The %dth solution.", i + 1);
}
return 0;
}