Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1864343
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: C/C++

2006-06-27 09:11:08

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) |
给主人留下些什么吧!~~