OS:FC3
Compiler: gcc version 3.4.2
#include
#include
#define RANK 8
char board[RANK][RANK];
int check_up(int row, int column)
{
int i = row -1;
// printf("check_up, %d, %d\n", row, column);
while(i >= 0)
{
if(board[i][column] == 'Q')
return 0;
i --;
}
return 1;
}
int check_up_left(int row, int column)
{
int i = row - 1, j = column - 1;
// printf("check_up_left, %d, %d\n", row, column);
while(i >= 0 && j >= 0)
{
if(board[i][j] == 'Q')
return 0;
i --;
j --;
}
return 1;
}
int check_up_right(int row, int column)
{
int i = row - 1, j = column + 1;
// printf("check_right, %d, %d\n", row, column);
while(i >= 0 && j <= 7)
{
if(board[i][j] == 'Q')
return 0;
i --;
j ++;
}
return 1;
}
int check(int row, int column)
{
int res = 0;;
// printf("check %d, %d\n", row, column);
if(row == 0)
return 1;
switch(column)
{
case 0:
res = (check_up(row,column)) && (check_up_right(row,column));
break;
case (RANK-1):
res = (check_up(row, column)) && (check_up_left(row,column));
break;
default:
res = (check_up(row, column)) && (check_up_left(row,column)) && (check_up_right(row,column));
break;
}
return res;
}
void show()
{
int i, j;
printf(" ");
for(i = 0; i < RANK; i ++)
printf("\33[4m \33[0m");
printf("\n");
for(i = 0; i < RANK; i ++)
{
printf("|");
for(j = 0; j < RANK ; j ++)
printf("\33[4m%c|", board[i][j]);
printf("\33[0m\n");
}
printf("\n");
}
void init_board()
{
int i, j;
for(i = 0; i < RANK; i ++)
for(j = 0; j < RANK ; j ++)
board[i][j] = ' ';
}
void empress(int row)
{
int i, j, k;
sleep(1);
if(row == RANK -1) //last row
{
for(i = 0; i < RANK; i ++)
{
if(check(row, i))
{
board[row][i] = 'Q';
show();
}
board[row][i] = ' ';
}
return ;
}
for(i = 0; i < RANK; i ++)
{
if(check(row, i))
{
for(k = row; k < RANK ; k ++)
for(j = 0; j < RANK ; j ++)
board[k][j] = ' ';
board[row][i] = 'Q';
empress(row+1);
}
}
}
int main()
{
init_board();
empress(0);
}
阅读(3258) | 评论(8) | 转发(0) |