帮忙看看这个程序--n皇后问题 出现段错误,但找不到那个判断错了
#include <stdio.h>char chessboard[8][8];
int queenplace(int, int);
int n_queens(int locx, int locy, int queens)
{
int i, j; //循环计数变量
int result = 0;
if (queens == 8) //递归结束条件
return 1;
else
if (queenplace(locx, locy)) //递归执行部分
{
chessboard[locx][locy] = 'Q';
for (i = 0; i < 8; i++)
for (j = 0; j < 8; i++){
result += n_queens(i, j, queens+1);
if(result > 0)
break;
}
if( result > 0)
return 1;
else{
chessboard[locx][locy] = 'x';
return 0;
}
}
else
return 0;
}
/*判断传入坐标是否可以放置皇后*/
int queenplace(int locx, int locy)
{
int i, j;
if (chessboard[locx][locy] != 'x')
return 0;
for (j = locy - 1; j >= 0; j-- ) //judge up
if (chessboard[locx][j] != 'x')
return 0;
for (j = locy + 1; j < 8; j++) //judge down
if (chessboard[locx][j] != 'x')
return 0;
for (i = locx - 1; i >= 0; i--) //judge left
if (chessboard[i][locy] != 'x')
return 0;
for (i = locx + 1; i < 8; i++) //judge right
if (chessboard[i][locy] != 'x')
return 0;
i = locx -1;
j = locy -1;
while (i >= 0 && j >= 0)
if (chessboard[i--][j--] != 'x') // judge left up
return 0;
i = locx + 1;
j = locy - 1;
while (i < 8 && j >= 0) //judge right up
if (chessboard[i++][j--] != 'x')
return 0;
i = locx - 1;
j = locy + 1;
while (i >= 0 && j < 8) //judge left down
if (chessboard[i--][j++] != 'x')
return 0;
i = locx + 1;
j = locy + 1;
while (i < 8 && j < 8) //judge right down
if (chessboard[i++][j++] != 'x')
return 0;
return 1;
}
int main()
{
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
chessboard[i][j] = 'x';
n_queens(0,0,0);
printf("the graph of 8 queens on the chessboard:\n");
printf(" 0 1 2 3 4 5 6 7\n");
printf(" +---+---+---+---+---+---+---+---+\n");
for (i = 0; i < 8; i++){
printf(" %d |",i);
for (j = 0; j < 8; j++)
printf("-%c-|",chessboard[i][j]);
printf("\n +---+---+---+---+---+---+---+---+\n");
}
return 0;
}