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 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...
随机推荐
- Scrum中的User Story
我们通常用User Story来描述Backlog里的各个Backlog项,User Story是从用户的角度对系统的某个功能模块所作的简短描述.一个User Story描述了项目中的一个小功能,以及 ...
- 2013流行Python项目汇总
2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...
- 关于用 MySQL 存储 Emoji
http://www.v2ex.com/t/137724 如果你希望让你的网站或者 App 支持 Emoji,那么在初次设置 MySQL 时,有一些细节你需要知道. Emoji Emoji 字符的特殊 ...
- live555源码研究(二)------TaskScheduler类
一.TaskScheduler类作用 1,他是使用环境的一部分. 2,他提供了对socket触发事件的管理. 二.类TaskScheduler继承关系图 二.TaskScheduler成员函数 1,s ...
- Servlet课程0426(十)Servlet如何删除cookie
//如何删除Cookie案例 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class Coo ...
- 安装Hadoop系列 — 安装JDK-8u5
安装步骤如下: 1)下载 JDK 8 从http://www.oracle.com/technetwork/java/javasebusiness/downloads/ 选择下载JDK的最新版本 JD ...
- Complete The Pattern #2
Complete The Pattern #2 Description: Task: You have to write a function pattern which creates the fo ...
- Java面试题-并发容器和框架
1. 如何让一段程序并发的执行,并最终汇总结果? 答:使用CyclicBarrier 和CountDownLatch都可以,使用CyclicBarrier 在多个关口处将多个线程执行结果汇总,Coun ...
- Angularjs中input的指令和属性
建议添加 novalidate属性(可无值)到form标签上,这样可以保证在表单不合法的情况下阻止浏览器继续提交数据. 可以给表单元素 input 添加 required 属性(可无值),让该表单成为 ...
- 需要保存数据zabbix,不需要保存数据nagios
需要保存数据zabbix,不需要保存数据nagios cacti 有什么好用的基于Web的Linux系统监控开源工具(网管系统) 要求类似于Ubuntu的Landscape,可以记录下历史CPU数值. ...