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. Creating a new Signiant Transfer Engine because the previous transfer had to be canceled.

    From: http://stackoverflow.com/questions/10548196/application-loader-new-weird-warning-about-signian ...

  2. 作为 .Net 攻城师,所必需掌握的 .Net Profiling 技术

    众所周知,性能问题是所有实用应用在迭代过程中必然要面对的问题.对于此类问题,简单地投入更多硬件资源的做法可能会取得一定效果.但总的来看,此类做法的边际成本是不断上升的.换言之,随着性能需求的上涨,要换 ...

  3. IsBadReadPtr|IsBadWritePtr调试崩溃

    遇到一未找到必然出现条件的崩溃,不知道什么时候能触发崩溃,崩溃dump显示,试图访问了非法的内存或者写入了非法的内存 此时如下两个函数就比较有用了: BOOL WINAPI IsBadReadPtr( ...

  4. hdu 4664 Triangulation 博弈论

    看到这题时,当时还不会做,也没搞懂sg函数,于是狠狠的钻研了下博弈论,渐渐的知道了sg函数…… 现在在来做这题就很容易了,1A 打表容易发现在80左右的时候就出现循环节了 代码如下: #include ...

  5. android 设置gridView item的高度

    public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater ...

  6. linux useradd添加用户

    useradd命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户. useradd -m -s /bin/bash -g 群组名 用户名 其中 -m:自动建立用户的登入目录. -s:指定用户 ...

  7. ibatis的there is no statement named xxx in this SqlMap

    报错情况如下: com.ibatis.sqlmap.client.SqlMapException: There is no statement named Control.insert-control ...

  8. NuGet相关的文章

    NuGet学习笔记(1)——初识NuGet及快速安装使用http://www.cnblogs.com/zhwl/p/3377510.html NuGet学习笔记(2) 使用图形化界面打包自己的类库ht ...

  9. A9逻辑编译问题

    root@phone-desktop:~# cd '/opt/BARE/01led' root@phone-desktop:/opt/BARE/01led# makearm-linux-gnueabi ...

  10. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...