import java.util.Arrays;

 public class Solution
 {
     public static void main(String[] args)
     {
         int[][] chessboard = new int[8][8];
         int[][] answerSave = new int[92][8];
         int[] oneAnswer = new int[8];

         while(true)
         {
             putQueens(chessboard);

             if(judgeCorrect(chessboard) == true)
             {
                 oneAnswer = convert(chessboard);
                 if(!contain(answerSave, oneAnswer))
                 {
                     drawChessboard(chessboard);
                     putIn(answerSave, oneAnswer);
                     if(isFull(answerSave))
                         break;
                 }
                 else
                     clearChessboard(chessboard);
             }
             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;
     }

     //convert the 2D chessboard in a 1D array
     public static int[] convert(int[][] array)
     {
         int[] oneDQueen = new int[8];
         for(int i = 0; i < 8; i++)
             for(int j = 0; j < 8; j++)
             {
                 if(array[i][j] == 1)
                     oneDQueen[i] = j;
             }
         return oneDQueen;
     }

     //judge if the answer has been found
     public static boolean contain(int[][] largeArray, int[] littleArray)
     {
         for(int[] array: largeArray)
             if(Arrays.equals(array, littleArray))
                 return true;
         return false;
     }

     //put the answer in the answer-save array
     public static void putIn(int[][] largeArray, int[] littleArray)
     {
         int signI = 0;
         for(int i = 0; i < 92; i++)
             for(int j = 0; j < 7; j++)
                 if(largeArray[i][j] == 0 && largeArray[i][j + 1] == 0)
                 {
                     signI = i;
                     putIn(largeArray, littleArray, signI);
                     return;
                 }
     } 

     public static void putIn(int[][] largeArray, int[] littleArray, int sign)
     {
         for(int i = 0; i < 8; i++)
             largeArray[sign][i] = littleArray[i];
     }

     //judge if the array is full
     public static boolean isFull(int[][] largeArray)
     {
         for(int j = 0; j < 8; j++)
             if(largeArray[91][j] != 0)
                 return true;
         return false;
     }

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

  1. CENTOS 6.5 平台离线编译安装 Mysql5.6.22

    一.下载源码包 http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.22.tar.gz 二.准备工作 卸载之前本机自带的MYSQL 安装 cmake,编 ...

  2. EC笔记:第4部分:22、所有成员都应该是private的

    EC笔记:第4部分:22.所有成员都应该是private的 更简单的访问 用户不用记得什么时候该带上括号,什么时候不用带上括号(因为很确定的就要带上括号) 访问限制 对于public的成员变量,我们可 ...

  3. Hadoop学习笔记—22.Hadoop2.x环境搭建与配置

    自从2015年花了2个多月时间把Hadoop1.x的学习教程学习了一遍,对Hadoop这个神奇的小象有了一个初步的了解,还对每次学习的内容进行了总结,也形成了我的一个博文系列<Hadoop学习笔 ...

  4. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  5. Fedora 22中的Services and Daemons

    Introduction Maintaining security on your system is extremely important, and one approach for this t ...

  6. Fedora 22中的RPM软件包管理工具

    Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...

  7. Fedora 22中的用户和用户组管理

    The control of users and groups is a core element of Fedora system administration. This chapter expl ...

  8. Fedora 22中的日期和时间配置

    Introduction Modern operating systems distinguish between the following two types of clocks: A real- ...

  9. Fedora 22中的DNF软件包管理工具

    Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...

随机推荐

  1. 深入了解nagios的各配置文件

    转自http://wolfword.blog.51cto.com/4892126/1220209   对每个配置文件进行讲解,深入理解nagios,好好学习,天天向上~ (1)templates.cf ...

  2. 到底该不该熟悉掌握struts2的ONGL呢?

    其实,在学习网站开发过程中,其实不掌握ONGL也是可以的.我们完全可以使用JSTL和EL来代替OGNL. 只要存在要往页面传输内容,我们直接把对象放在request范围即可,这样我们就可以在jsp中使 ...

  3. Java发送post请求

    package com.baoxiu.test; import java.io.BufferedReader;import java.io.InputStreamReader;import java. ...

  4. codeforces #305 B Mike and Feet

    跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...

  5. 设置window窗口的背景色为护眼色

    win7设置:桌面右键 -> 个性化 -> 窗口颜色 -> 高级外观设置 ->  '项目'下拉菜单 ->  '窗口'

  6. 跨平台的目录遍历实现方法(windows和linux已经测试)

    dirent.h是gcc下的一个头文件,在windows中是没有的.这个文件中封装了几个对目录进行操作函数: static DIR *opendir (const char *dirname);sta ...

  7. 编译qt-mobility

    因为用到了qt-mobility,必须自己编译一下,参考列出了参考资料. 参考: 1. windows下编译qt-mobility  http://hi.baidu.com/xchinux/blog/ ...

  8. 120. Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  9. 转载 .htaccess文件RewriteRule语法规则

    详见: http://blog.csdn.net/scchary/article/details/40045807    # -- 位于行首时表示注释.       [F] -- Forbidden( ...

  10. ADT中的代码补全设置

    设置自动补全代码 刚刚学Android,有很多变量和方法 都不熟悉.需要有提示,才更加方便. 快捷方式:Alt + /    可以出现代码提示. 默认的只有输入“ .” 以后才会有代码补全提示,可作如 ...