HW6.20

public class Solution
{
public static void main(String[] args)
{
int[][] chessboard = new int[8][8];
while(true)
{
putQueens(chessboard);
if(judgeCorrect(chessboard) == true)
{
drawChessboard(chessboard);
break;
}
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;
}
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.20的更多相关文章
- CSharpGL(20)用unProject和Project实现鼠标拖拽图元
CSharpGL(20)用unProject和Project实现鼠标拖拽图元 效果图 例如,你可以把Big Dipper这个模型拽成下面这个样子. 配合旋转,还可以继续拖拽成这样. 当然,能拖拽的不只 ...
- ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi
点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...
- 帮我做个APP,给你20万,做不做?
一.为什么要写这篇文章 前段时间,有个辞职 创业的同事(做法务的) 问我 开发一个 新闻类的APP要多少钱,产品.UI.接口.后台管理页 他们啥都没有,想全部外包. 我 并没有在外包公司做过,也没 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单
系列目录 不知不觉到20讲,真是漫长的日子,可惜最近工作挺忙,要不可以有更多的时间来更新,多谢大家的一路支持.如果你觉得好,记得帮我点击推荐^-^ 我们在之前已经插入一些真实数据,其中包含了一个用户和 ...
- LINQ to SQL语句(20)之存储过程
在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在数据库中, ...
- C#开发微信门户及应用(20)-微信企业号的菜单管理
前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...
- 20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...
- 20个不可思议的 WebGL 示例和演示
WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实现3D图像的展示.WebGL 可以为 Canvas 提供硬件3D加速渲 ...
- 20款 JavaScript 开发框架推荐给前端开发者
下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...
随机推荐
- FiddlerScript开发
1.为Fiddler会话列表添加自定义列 只需要为你的方法(方法名任意)添加BindUIColumn Attribute 就可以添加自定义列到Session List,下面的代码添加Method列到会 ...
- Python 开源异步并发框架的未来
http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...
- struts2 标签的使用之二 s:iterator
struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:s:iterator 标签有3个属性: value:被迭代的集合 id :指定集 ...
- codeforces #309 div1 A
先说我的解法吧 首先设f(i,j)表示选了前i个球且j种颜色都已经选完了的方案数 这显然是可以随便转移的 #include<cstdio> #include<cstring> ...
- HeadFirst设计模式之观察者模式
一.什么是观察者模式 观察者模式定义了一系列对象间一对多的关系,当主题对象的状态发生变化时,会通知所有观察者 二.自定义观察模式 1. 2. package headfirst.designpatte ...
- HDU1171——Big Event in HDU(母函数)
Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...
- python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
文件夹里有多个RM格式的视频文件,现需要把它们的文件名都提取出来,并去掉文件的扩展名,以便放到需要的网页里. 源代码: # --- picknames.py --- import os filenam ...
- 安装Hadoop系列 — 导入Hadoop源码项目
将Hadoop源码导入Eclipse有个最大好处就是通过 "ctrl + shift + r" 可以快速打开Hadoop源码文件. 第一步:在Eclipse新建一个Java项目,h ...
- Android 内核初识(2)android系统架构
以模块角度 以Java,native,kernel角度
- C#中this在构造函数时的使用
今天编程的时候,想要用this来处理构造函数,想了半天没有想起来 后来找了自己以前记录的 http://www.cnblogs.com/chucklu/p/4842766.html public Cu ...