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的更多相关文章

  1. CSharpGL(20)用unProject和Project实现鼠标拖拽图元

    CSharpGL(20)用unProject和Project实现鼠标拖拽图元 效果图 例如,你可以把Big Dipper这个模型拽成下面这个样子. 配合旋转,还可以继续拖拽成这样. 当然,能拖拽的不只 ...

  2. ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi

    点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...

  3. 帮我做个APP,给你20万,做不做?

    一.为什么要写这篇文章 前段时间,有个辞职 创业的同事(做法务的)  问我 开发一个 新闻类的APP要多少钱,产品.UI.接口.后台管理页  他们啥都没有,想全部外包. 我 并没有在外包公司做过,也没 ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单

    系列目录 不知不觉到20讲,真是漫长的日子,可惜最近工作挺忙,要不可以有更多的时间来更新,多谢大家的一路支持.如果你觉得好,记得帮我点击推荐^-^ 我们在之前已经插入一些真实数据,其中包含了一个用户和 ...

  5. LINQ to SQL语句(20)之存储过程

    在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在数据库中, ...

  6. C#开发微信门户及应用(20)-微信企业号的菜单管理

    前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...

  7. 20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

  8. 20个不可思议的 WebGL 示例和演示

    WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实现3D图像的展示.WebGL 可以为 Canvas 提供硬件3D加速渲 ...

  9. 20款 JavaScript 开发框架推荐给前端开发者

    下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...

随机推荐

  1. HZNU1015: 矩阵排序

    http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1015 题意:把矩阵每一行都排序. (以前觉得很难的题目回头看看原来如此简单 ][]; ; i&l ...

  2. HTML字符实体(Character Entities),转义字符串(Escape Sequence)

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  3. MVC开发过程中的疑难杂症

    MVC使用客户端验证 <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type=& ...

  4. *[topcoder]JumpFurther

    http://community.topcoder.com/stat?c=problem_statement&pm=12300&rd=15699 题意:有一个无限长的阶梯,i从1到N, ...

  5. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  6. SQLite入门与分析(四)---Page Cache之事务处理(3)

    写在前面:由于内容较多,所以断续没有写完的内容. 11.删除日志文件(Deleting The Rollback Journal)一旦更改写入设备,日志文件将会被删除,这是事务真正提交的时刻.如果在这 ...

  7. altium designer 13 学习之添加汉字

    在altium desginer中如果你是想添加英文还是比较方便的,基本直接就可以输入了,但是添加中文就不是那么简单了,下面不介绍下如何在altium designer中快速的添加自己想要的中文 工具 ...

  8. LinkedList源代码深入剖析

    第1部分 LinkedList介绍LinkedList简介 public class LinkedList<E> extends AbstractSequentialList<E&g ...

  9. 如何快速查看linux的发行版信息

    思路一: 在CentOS中想查看发行版信息,输入了lsb_release -a 命令却报错了,通过输入以下命令进行安装 yum install redhat-lsb -y 然后继续查看发行版信息 [r ...

  10. BIRT使用1:简介、概念、元素、报表设计器组成

    前一篇博客对birt进行了一个初探,相信通过上篇博客大家对birt有个初步认识,接下来我们随着下面这张思维导图的展示,进入birt的使用学习. 这一篇博客是第一部分,主要介绍一下birt的简介.概念. ...