蓝桥杯 算法训练 王、后传说
来源:互联网 发布:怪你过分美丽歌词知乎 编辑:程序博客网 时间:2024/04/25 14:28
看过清宫戏的中国人都知道,后宫乃步步惊心的险恶之地。各皇后都有自己的势力范围,但也总能找到相安无事的办法。
所有中国人都知道,皇权神圣,伴君如伴虎,触龙颜者死......
现在有一个n*n的皇宫,国王占据他所在位置及周围的共9个格子,这些格子皇后不能使用(如果国王在王宫的边上,占用的格子可能不到9个)。当然,皇后也不会攻击国王。
现在知道了国王的位置(x,y)(国王位于第x行第y列,x,y的起始行和列为1),请问,有多少种方案放置n个皇后,使她们不能互相攻击。
n<=12
#include <stdio.h>
#define INF 1<<30
#define MAXSIZE 20
int n, x, y, ans;
int palace[MAXSIZE][MAXSIZE];
void init()
{
int i, j;
for(i = 0; i < MAXSIZE; i ++){
for(j = 0; j < MAXSIZE; j ++){
palace[i][j] = 0;
}
}
for(i = x-1; i <= x+1; i++){
for(j = y-1; j <= y+1; j ++){
palace[i][j] = INF;
}
}
ans = 0;
}
int judge(int row, int col)
{
int i, j;
for(i = row-1, j = col-1; i >= 1 && j >= 1; i --, j --){
if(palace[i][j] == 1){
return 0;
}
}
for(i = row-1, j = col+1; i >= 1 && j <= n; i --, j ++){
if(palace[i][j] == 1){
return 0;
}
}
for(i = row-1, j = col; i >= 1; i --){
if(palace[i][j] == 1){
return 0;
}
}
return 1;
}
void countScheme(int r)
{
if(r > n){
ans ++;
return;
}
int i;
for(i = 1; i <= n; i ++){
if(palace[r][i] == INF){
continue;
}
if(judge(r, i)){
palace[r][i] = 1;
countScheme(r+1);
palace[r][i] = 0;
}else{
continue;
}
}
}
int main()
{
scanf("%d%d%d", &n, &x, &y);
init();
countScheme(1);
printf("%d", ans);
return 0;
}
- 蓝桥杯 算法训练 王、后传说
- 算法训练 王、后传说 蓝桥杯
- 蓝桥杯-算法训练- 王、后传说
- 算法训练 王、后传说
- 算法训练 王、后传说
- 算法训练 王、后传说
- 算法训练 王、后传说
- 算法训练 王、后传说
- 算法训练 王、后传说
- 蓝桥杯ALGO-125算法训练 王、后传说(回溯、递归)
- 蓝桥杯 算法训练 王、后传说 八皇后问题
- 算法训练 王、后传说 (八皇后问题)
- 王、后传说——蓝桥杯
- 王、后传说 (回溯,八皇后)
- 蓝桥杯算法训练题解
- 蓝桥杯 算法训练 排序
- 蓝桥杯 算法训练 暗恋
- 蓝桥杯 算法训练 数列
- MongoDB 定时备份
- linux的3个时间点的定义和使用方法 Access,Modify,Change
- (Ryan的Redis系列博客)8.Redis的散列
- edittext的setOnKeyListener里面的方法响应两次的原因和解决方法
- 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
- 蓝桥杯 算法训练 王、后传说
- React-native start ERROR Packager can't listen on port 8081
- BZOJ 1854, 游戏
- javascript 设计模式-单体模式
- CocoaPods三方框架
- codeforces 735D Taxes 哥德巴赫猜想
- React-Native中Image请求网络图片时占位图的设置
- <body>标签,网页上显示的内容放在这里
- cocos2d-x v2 和 v3 对应手册