如何基于java语言实现八皇后问题
作者:东溪陈姓少年 时间:2022-08-22 09:05:25
这篇文章主要介绍了如何基于java语言实现八皇后问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
八皇后问题,在一个8X8的棋盘中,放置八个棋子,每个棋子的上下左右,左上左下,右上右下方向上不得有其他棋子。正确答案为92中,接下来用java语言实现。
代码如下
package eightQuen;
/**
* 八皇后问题
*
* @author 83771
*
*/
public class eight {
// 定义一个数组 表示棋盘
public static Integer[][] checkerBoard = new Integer[8][8];
// 棋盘副本
public static Integer[][] checkerBoardCopy = new Integer[8][8];
// 计数器 用于计数有多少种方法
public static Integer jishu = 1;
// 定义横竖斜方向上是否有棋子
public static boolean flag1 = true;
public static boolean flag2 = true;
public static boolean flag3 = true;
public static boolean flag4 = true;
// 初始化一个棋盘 8x8
public static void init() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(0 + " ");
checkerBoard[i][j] = 0;
}
System.out.println();
}
checkerBoardCopy = checkerBoard;
}
// 递归测试方法
public static void startTest(int row) {
for (int col = 0; col < 8; col++) {
if (checkCheet(row, col, checkerBoardCopy) == 1) {
if (row < 7) {
startTest(++row);
--row;
}
}
// 该行重新赋值为0 进行下一次判断
checkerBoardCopy[row][col] = 0;
}
}
// 检查是否危险
// row行
// col列
public static int checkCheet(int row, int col, Integer[][] checkerBoardCopy) {
flag1 = true;
flag2 = true;
flag3 = true;
flag4 = true;
// 行方向上是否满足条件
for (int i = 0; i < 8; i++) {
if (checkerBoardCopy[row][i] == 1) {
flag1 = false;
break;
}
}
// 列方向上是否满足条件
for (int j = 0; j < 8; j++) {
if (checkerBoardCopy[j][col] == 1) {
flag2 = false;
break;
}
}
// 右下方向
for (int i = row, j = col; i < 8 & j < 8; i++, j++) {
if (checkerBoardCopy[i][j] == 1) {
flag3 = false;
break;
}
}
// 左上方向
for (int i = row, j = col; i >= 0 & j >= 0; i--, j--) {
if (checkerBoardCopy[i][j] == 1) {
flag3 = false;
break;
}
}
// 左下方向
for (int i = row, j = col; i < 8 & j >= 0; i++, j--) {
if (checkerBoardCopy[i][j] == 1) {
flag4 = false;
break;
}
}
// 右上方向
for (int i = row, j = col; i >= 0 & j < 8; i--, j++) {
if (checkerBoardCopy[i][j] == 1) {
flag4 = false;
break;
}
}
if (flag1 & flag2 & flag3 & flag4) {
// 若为真 增此点的值赋为1
checkerBoardCopy[row][col] = 1;
// 如果已经判断到最后一行 并且最后一行也符合情况 打印整个棋盘
if (row == 7) {
printCheets(checkerBoardCopy);
}
return 1;
}
return 0;
}
// 打印棋盘方法
public static void printCheets(Integer[][] checkerBoardCopy) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(checkerBoardCopy[i][j] + " ");
}
System.out.println();
}
System.out.println("=================" + jishu++);
}
public static void main(String[] args) {
init();
startTest(0);
}
}
copy后可直接运行。 记一下这次的代码。
来源:https://www.cnblogs.com/dongxishaonian/p/12038891.html
标签:java,语言,八皇后,问题
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现带动画效果的可点击展开TextView
2023-08-06 05:42:08
![](https://img.aspxhome.com/file/2023/9/85989_0s.png)
Java动态获取实现某个接口下所有的实现类对象集合
2023-04-01 14:43:20
Java实现的不同图片居中剪裁生成同一尺寸缩略图功能示例
2023-08-23 14:53:15
详解springboot之jackson的两种配置方式
2021-11-03 11:01:21
java中SynchronizedList和Vector的区别详解
2023-08-23 10:13:12
Java读取json数据并存入数据库的操作代码
2023-09-23 06:00:57
java读取XML文件的四种方法总结(必看篇)
2023-03-22 23:12:58
详解java 客户端链接不上redis解决方案
2023-11-12 10:12:15
![](https://img.aspxhome.com/file/2023/4/59204_0s.png)
elasticsearch分布式及数据的功能源码分析
2023-08-11 06:31:26
![](https://img.aspxhome.com/file/2023/7/58087_0s.png)
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
springboot连接不上redis的三种解决办法
2022-03-21 20:57:23
![](https://img.aspxhome.com/file/2023/3/67093_0s.png)
fastjson转换对象实体@JsonProperty不生效问题及解决
2023-10-07 00:13:51
![](https://img.aspxhome.com/file/2023/2/65312_0s.png)
举例详解用Java实现web分页功能的方法
2021-07-27 10:50:19
使用eclipse创建java项目的方法
2022-10-15 04:41:00
![](https://img.aspxhome.com/file/2023/0/64010_0s.jpg)
SpringBoot获取配置文件内容的几种方式总结
2023-11-24 18:10:48
Swagger2配置方式(解决404报错)
2022-08-30 17:21:13
![](https://img.aspxhome.com/file/2023/0/63660_0s.png)
分析并发编程之LongAdder原理
2023-05-11 17:19:30
![](https://img.aspxhome.com/file/2023/9/65599_0s.jpg)
spring的Cache注解和redis的区别说明
2023-10-20 02:01:33
![](https://img.aspxhome.com/file/2023/9/62969_0s.png)
IDEA报错:无效的源发行版解决方案
2022-06-05 08:38:58
![](https://img.aspxhome.com/file/2023/4/62454_0s.png)
java实现简单石头剪刀布小游戏
2021-09-17 07:25:41
![](https://img.aspxhome.com/file/2023/4/63784_0s.jpg)