Chinaunix首页 | 论坛 | 博客
  • 博客访问: 132949
  • 博文数量: 75
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 820
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-07 08:31
文章分类

全部博文(75)

文章存档

2011年(53)

2010年(22)

我的朋友

分类:

2010-12-20 20:41:37

Just for learning..
 
 

#include <stdlib.h>
#include <stdio.h>

#define SIZE 8

void printqueen(int **a, int col, int row);
int queen(int **arr, int col, int row);
void lost(int y);

static int a[8][8] = {
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0}
                    };
static int *p = &a[0][0];
static int **pp = &p;

int main(void){
    printqueen(pp, 8, 8);
    lost(0);
    printqueen(pp, 8, 8);
    return 0;
}

void printqueen(int **arr, int col, int row){
    int a, b;
    for(a = 0;a < col;a++)
    {
        for(b = 0; b < row; b++)
        {
            if(*(*arr + SIZE*a + b) != 0)
                printf("* ");
            else
                printf(". ");
        }
        putchar('\n');
    }
    puts("-----------------\n");
}

//if can be falldown, return 1, else return 0

int queen(int **arr, int col, int row){
    int x, y;// x = row, y = col

    for(x = 0; x < SIZE; x++)
    {
        if(x == row) continue;
        if(*(*arr + row*SIZE + x) == 1) return 0;
    }

    for(y = 0; y < SIZE; y++)
    {
        if(y == col) continue;
        if(*(*arr + y*SIZE + col) == 1) return 0;
    }

    for(x = 0; x < SIZE; x++)
        for(y = 0; y < SIZE; y++)
        {
            if(x == row && y == col) continue;
            if( ((x + y) == (row + col)) || ((y - x) == (col - row)) ){
                if(*(*arr + x*SIZE + y) == 1) return 0;
            }
        }
    return 1;
}

void lost(int y){
    int x;
    if(y > SIZE) return;
    for(x = 0; x < SIZE; x++)
    {
        if(queen(pp, x, y)){
            *(*pp + y*SIZE + x) = 1;
            lost(y+1);
        }
    }
}


阅读(154) | 评论(0) | 转发(0) |
0

上一篇:For fun 20101217

下一篇:my code 20101220_2

给主人留下些什么吧!~~