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 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...
随机推荐
- C++: std::string 与 Unicode 如何结合?
关键字:std::string Unicode 转自:http://www.vckbase.com/document/viewdoc/?id=1293 一旦知道 TCHAR 和_T 是如何工作的,那么 ...
- hdu 2897 邂逅明下 博弈论
这个分区间: 1<=n%(p+q)<=p 必败 其他必胜! 代码如下: #include<iostream> #include<stdio.h> #include& ...
- 深入理解JVM--JVM垃圾回收机制
Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放空间,既要写构造函数,又要写析构函数,很多时候都 ...
- adobe 蛋疼的套装, 想安装一个Flash Professional CS6,标准版还没有...
产品比较 查看内容 查看各 Creative Suite 6 版本的组件. Design Standard Design & Web Premium Production Premium Ma ...
- 编程添加"作为服务登录”权利(包括例子和API)
搜索"log on as a service programmatically" https://msdn.microsoft.com/en-us/library/windows/ ...
- SGU 130
SGU130,用k条弦将一个圆分成k+1份的方法数. #include <iostream> #include <vector> #include <string> ...
- xp宿主机和VMware下Ubuntu12.04共享文件夹
VMware下Windows与Linux共享文件的方法有很多,比如Samba等等,我这里介绍简单地通过设置VMware来达到共享的目的. 打开VMware的设置,在"options" ...
- C++ 空类默认产生成员函数
class Empty { Empty(){...} //默认构造函数 ~Empty(){...} //默认析构函数 Empty(const Empty&){...} //拷贝构造函数 Emp ...
- Visual Studio Developer Command Prompt删除localdb的方法
PM> sqllocaldb.exe stop v11. LocalDB instance "v11.0" stopped. PM> sqllocaldb.exe de ...
- hdu 3367 Pseudoforest
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...