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 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...
随机推荐
- (转)关于Struts 2 拦截器参数丢失问题
from:http://www.cnblogs.com/huzx/archive/2011/06/09/2076328.html 今天在做用户的登陆认证的时候出现的问题. 在传参数的时候,发现参数丢失 ...
- DeepFace--Facebook的人脸识别(转)
DeepFace基本框架 人脸识别的基本流程是: detect -> aligh -> represent -> classify 人脸对齐流程 分为如下几步: a. 人脸检测,使用 ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
- codeforces #310 div1 A
首先我们考虑最暴力的拆解拼凑 显然拆分掉所有的链需要 n-m 次 之后拼凑需要 n-1 次 然后由题目规定可知:只有从1出发且连续的链不用拆掉,其余的都必须拆掉(因为两个都套有娃娃的套娃不能组合) 我 ...
- Android:控件布局(绝对布局)AbsoluteLayout
绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x --------组件x坐标 andr ...
- Eclipse中安装使用SVN
参考网址: Eclipse中使用SVN - 流逝的是岁月,沉淀的是经典 - 博客频道 - CSDN.NET http://blog.csdn.net/v123411739/article/detail ...
- Maven中的一点疑惑的地方
我们建立一个Maven项目,其结构如下:
- 107. Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- MySQL复制应用中继日志解析
前言:SQL线程应用中继日志,在binlog_format是row格式的时候,是居于主键更新,下面结合一张图来证明 1.从一个大神那边得到一张图片,SQL线程应用中继日志流程,下面就实验验证一下:(P ...
- 【HDOJ】4691 Front compression
后缀数组基础题目,dc3解. /* 4691 */ #include <iostream> #include <sstream> #include <string> ...