当前位置: 首页 > news >正文

Chessboard and Queens

题目描述

Your task is to place eight queens on a chessboard so that no two queens are attacking each other. As an additional challenge, each square is either free or reserved, and you can only place queens on the free squares. However, the reserved squares do not prevent queens from attacking each other.

How many possible ways are there to place the queens?

输入

The input has eight lines, and each of them has eight characters. Each square is either free (.) or reserved (*).

输出

Print one integer: the number of ways you can place the queens.

样例输入 
........
........
..*.....
........
........
.....**.
...*....
........
样例输出
65
思路分析

1.任意两皇后不能放在同一行、同一列、同一对角线上

2.采用回溯算法解决带障碍物的八皇后问题,通过递归和条件判断找出所有可行的放置方案并统计数量。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
char chessboard[8][8];
ll ans,col[8];
int issafe(int i,int j){if(i==0)return 1;else{for(int k=0;k<i;k++){int a=abs(i-k),b=abs(j-col[k]);if(a==b||b==0)return 0;}return 1;}
}
void solve(int i){for(int j=0;j<8;j++){if(chessboard[i][j]!='*'){if(issafe(i,j)){if(i==7)ans++;else{col[i]=j;solve(i+1);}}}}
}
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);for(int i=0;i<8;i++){for(int j=0;j<8;j++){cin>>chessboard[i][j];}}solve(0);cout<<ans;return 0;
}

http://www.xdnf.cn/news/1193869.html

相关文章:

  • 磁悬浮轴承(AMB)系统中引入转速频率陷波器失稳分析
  • MySQL 中 VARCHAR(50) 和 VARCHAR(500) 的区别
  • docker 从主机复制文件到容器外进行编辑
  • Pspice仿真电路:(三十四)如何使用Pspcie进行仿真
  • 定义域第一题
  • 速通python加密之SHA加密
  • SpringBoot总结
  • 广东省省考备考(第五十七天7.26)——数量、言语(强化训练)
  • 如何做数据增强?
  • 大模型面试回答,介绍项目
  • 高性能网络DPDK、RDMA、XDP初探
  • URL与URI:互联网世界的“门牌号“与“身份证“
  • 网络编程2(应用层协议,传输层协议)
  • 「iOS」————继承链与对象的结构
  • SWC 深入全面讲解
  • 口腔助手|口腔挂号预约小程序|基于微信小程序的口腔门诊预约系统的设计与实现(源码+数据库+文档)
  • GitHub的免费账户的存储空间有多少?
  • 基于深度学习的图像分类:使用Capsule Networks实现高效分类
  • 网安-中间件-Redis未授权访问漏洞
  • 根本是什么
  • LeetCode 854:相似度为 K 的字符串
  • Flutter控件归纳总结
  • Jenkins运行pytest时指令失效的原因以及解决办法
  • useCallback/useMemo
  • 大模型蒸馏(distillation)---从DeepseekR1-1.5B到Qwen-2.5-1.5B蒸馏
  • ARM SMMUv3控制器注册过程分析(八)
  • 二分函数 lower_bound upper_bound
  • 21-ospf多区域
  • 【Bluedroid】btif_av_sink_execute_service之服务器禁用源码流程解析
  • Apache Doris Data Agent 解决方案:开启智能运维与数据治理新纪元