HW6.22

import java.util.Arrays;
public class Solution
{
public static void main(String[] args)
{
int[][] chessboard = new int[8][8];
int[][] answerSave = new int[92][8];
int[] oneAnswer = new int[8];
while(true)
{
putQueens(chessboard);
if(judgeCorrect(chessboard) == true)
{
oneAnswer = convert(chessboard);
if(!contain(answerSave, oneAnswer))
{
drawChessboard(chessboard);
putIn(answerSave, oneAnswer);
if(isFull(answerSave))
break;
}
else
clearChessboard(chessboard);
}
else
clearChessboard(chessboard);
}
}
//put 8 queens on the chessboard randomly
public static void putQueens(int[][] array)
{
int position;
for(int i = 0; i < 8; i++)
{
position = (int)(Math.random() * 8);
array[i][position] = 1;
}
}
//clear the chessboard
public static void clearChessboard(int[][] array)
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
array[i][j] = 0;
}
//judge if there is only one chess in a row
public static boolean judgeRow(int[][] array, int x, int y)
{
for(int i = y - 1; i >= 0; i--)
if(array[x][i] == 1)
return false;
for(int i = y + 1; i < 8; i++)
if(array[x][i] == 1)
return false;
return true;
}
//judge if there is only one chess in a column
public static boolean judgeColumn(int[][] array, int x, int y)
{
for(int i = x - 1; i >= 0; i--)
if(array[i][y] == 1)
return false;
for(int i = x + 1; i < 8; i++)
if(array[i][y] == 1)
return false;
return true;
}
//judge if there is only one chess in the lean from left-top to right-bottom
public static boolean judgeLeanTopToBottom(int[][] array, int x, int y)
{
for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--)
if(array[i][j] == 1)
return false;
for(int i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++)
if(array[i][j] == 1)
return false;
return true;
}
//judge if there is only one chess in the lean from left-bottom to right-top
public static boolean judgeLeanBottomToTop(int[][] array, int x, int y)
{
for(int i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--)
if(array[i][j] == 1)
return false;
for(int i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++)
if(array[i][j] == 1)
return false;
return true;
}
//judge if all the queens are put correctly
public static boolean judgeCorrect(int[][] array)
{
int[] putX = new int[8];
int[] putY = new int[8];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
if(array[i][j] == 1)
{
putX[i] = i;
putY[i] = j;
break;
}
for(int i = 0; i < 8; i++)
{
if(!(judgeRow(array, putX[i], putY[i]) && judgeColumn(array, putX[i], putY[i])
&& judgeLeanTopToBottom(array, putX[i], putY[i]) && judgeLeanBottomToTop(array, putX[i], putY[i])))
return false;
}
return true;
}
//convert the 2D chessboard in a 1D array
public static int[] convert(int[][] array)
{
int[] oneDQueen = new int[8];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
{
if(array[i][j] == 1)
oneDQueen[i] = j;
}
return oneDQueen;
}
//judge if the answer has been found
public static boolean contain(int[][] largeArray, int[] littleArray)
{
for(int[] array: largeArray)
if(Arrays.equals(array, littleArray))
return true;
return false;
}
//put the answer in the answer-save array
public static void putIn(int[][] largeArray, int[] littleArray)
{
int signI = 0;
for(int i = 0; i < 92; i++)
for(int j = 0; j < 7; j++)
if(largeArray[i][j] == 0 && largeArray[i][j + 1] == 0)
{
signI = i;
putIn(largeArray, littleArray, signI);
return;
}
}
public static void putIn(int[][] largeArray, int[] littleArray, int sign)
{
for(int i = 0; i < 8; i++)
largeArray[sign][i] = littleArray[i];
}
//judge if the array is full
public static boolean isFull(int[][] largeArray)
{
for(int j = 0; j < 8; j++)
if(largeArray[91][j] != 0)
return true;
return false;
}
public static void drawChessboard(int[][] array)
{
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
if(array[i][j] == 1)
System.out.print("Q");
else
System.out.print("-");
}
System.out.println();
}
}
}
HW6.22的更多相关文章
- CENTOS 6.5 平台离线编译安装 Mysql5.6.22
一.下载源码包 http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.22.tar.gz 二.准备工作 卸载之前本机自带的MYSQL 安装 cmake,编 ...
- EC笔记:第4部分:22、所有成员都应该是private的
EC笔记:第4部分:22.所有成员都应该是private的 更简单的访问 用户不用记得什么时候该带上括号,什么时候不用带上括号(因为很确定的就要带上括号) 访问限制 对于public的成员变量,我们可 ...
- Hadoop学习笔记—22.Hadoop2.x环境搭建与配置
自从2015年花了2个多月时间把Hadoop1.x的学习教程学习了一遍,对Hadoop这个神奇的小象有了一个初步的了解,还对每次学习的内容进行了总结,也形成了我的一个博文系列<Hadoop学习笔 ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- Fedora 22中的Services and Daemons
Introduction Maintaining security on your system is extremely important, and one approach for this t ...
- Fedora 22中的RPM软件包管理工具
Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...
- Fedora 22中的用户和用户组管理
The control of users and groups is a core element of Fedora system administration. This chapter expl ...
- Fedora 22中的日期和时间配置
Introduction Modern operating systems distinguish between the following two types of clocks: A real- ...
- Fedora 22中的DNF软件包管理工具
Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...
随机推荐
- spoj 2148
看似很水 却wa了好多遍 spoj上果然没有一下可以水过去的题....... #include<cstdio> #include<cstring> #include< ...
- Android 环境搭建 版本问题
jdk1.6 1.7 eclipse 3.7.2 SDK-r12 ADT 12 SDK和ADT必须配套 搭建环境需要四个软件: 1.JDK(这是最新版本jdk1.7官方下载地址:http:// ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- Google Play市场考察报告
考察了Google Play日本市场的10款应用,考察的重点在于每个App有什么亮点,盈利模式在哪里.本文并不是App的功能介绍. (1)恋爱文集[文库类应用] 该应用收录了一些恋爱文章,其主要受众是 ...
- linux bash_profile和.bashrc区别
经常在一些技术类的文章中提到修改bash_profile和.bashrc这两个文件,也算是使用频率比较高的两个文件吧,但实现同样一个功能,有的教程里说修改bash_profile这个文件,有的教程里却 ...
- 万网空间如何安装wordpress
万网空间如何安装wordpress建站教程 _ 学做网站论坛 http://www.xuewangzhan.com/wpbbs/1643.html 1.先在本地下载一个最新版本的wordpress ...
- WPF中动态添加xaml资源文件
一.新建一个资源文件,然后设置其Build Actoin(生成操作)为Resource(资源): 二.在App.xaml.cs的StartUp事件或者是你需要的时机代码段写上如下代码: Resourc ...
- Armitage主屏幕说明与命令行启动
(1)我们将Armitage主屏幕标注为A.B和C A:该区域显示预配置的模块.您可以在模块列表下面的文本框中输入要查找的模块进行查找. B:该区域显示我们可以进行漏洞测试的活跃主机. C:该区域显示 ...
- 如何提高Web服务端并发效率的异步编程技术
作为一名web工程师都希望自己做的web应用能被越来越多的人使用,如果我们所做的web应用随着用户的增多而宕机了,那么越来越多的人就会变得越来越少了,为了让我们的web应用能有更多人使用,我们就得提升 ...
- Nandflash 驱动移植
前段时间,研究了一下4G的Nandflash驱动.手头上只有飞凌6410BSP自带的Nandflash驱动,该驱动不支持K9GAG08U0D(2G)和K9LBG08U0D(4G)的Nandflash. ...