您的位置:首页 > 其它

8皇后问题(可以是其他数字的皇后)(转载注明出处谢谢!!)

2014-06-02 16:48 302 查看
#include<stdio.h>

#include <math.h>

#include <string>

#define N 8//改变数字代表几个皇后

char board

;

int col
;

int t;

int safetyPlace(int x,int y)

{

int i,j;

for(i = 0; i < x;i++)

{

j =col[i];

if(x==i||y==j)

return 0;

if(x-y==i-j||x+y==i+j)//判断左右对角线

return 0;

}

return 1;

}

void get_position(int i)

{

int w,j;

char a[1]={3};

if(i==N)

{

for(w=0;w<N;w++)

{

for(j=0;j<N;j++)

{

if(board[w][j]==001)

printf("%c ",board[w][j]);

else

{

printf("%c",a[0]);

printf("%c ",board[w][j]);

}

}

printf("\n");

}

printf("----------------------------\n");

t++;

}

else

{

int u;

for(u=0;u<N;u++)

{

if(safetyPlace(i,u)==1)

{

col[i]=u;

board[i][u]=001;

get_position(i+1);

col[i]=0;

board[i][u]=0;

}

}

}

}

int main()

{

printf("%c是皇后!\n\n",001);

get_position(0);

printf("一共%d种方法\n",t);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐