蓝桥杯—受伤的皇后(C语言解法)
题目描述
点击查看原题
思路描述
- 因为有n个皇后,n*n个格子,所以每行都会有一个皇后,所以用一维数组st存储每位皇后所在的列(st下标表示第几位皇后,也就是行数)
- 在正对角线上时,行列相减相等,在负对角线时,行列相加相等
- 确定每个皇后所在的列,通过查看该位皇后与前一皇后所在列是否冲突确定
代码
#include
#include
int n;
int res;//符合条件的结果数
int st[20];
int check(int r,int c){
int i;
int t1,t2,t3,t4;
for(i=1;in){
res++;
return;
}
for(i=1;i<=n;i++){//遍历列
if(check(h,i)){
st[h]=i;
dfs(h+1);
}
}
}
int main(){
scanf("%d",&n);
dfs(1);
printf("%d",res);
return 0;
}