HW6.22

  1 import java.util.Arrays;
  2
  3 public class Solution
  4 {
  5     public static void main(String[] args)
  6     {
  7         int[][] chessboard = new int[8][8];
  8         int[][] answerSave = new int[92][8];
  9         int[] oneAnswer = new int[8];
 10
 11         while(true)
 12         {
 13             putQueens(chessboard);
 14
 15             if(judgeCorrect(chessboard) == true)
 16             {
 17                 oneAnswer = convert(chessboard);
 18                 if(!contain(answerSave, oneAnswer))
 19                 {
 20                     drawChessboard(chessboard);
 21                     putIn(answerSave, oneAnswer);
 22                     if(isFull(answerSave))
 23                         break;
 24                 }
 25                 else
 26                     clearChessboard(chessboard);
 27             }
 28             else
 29                 clearChessboard(chessboard);
 30         }
 31     }
 32
 33
 34     //put 8 queens on the chessboard randomly
 35     public static void putQueens(int[][] array)
 36     {
 37         int position;
 38
 39         for(int i = 0; i < 8; i++)
 40         {
 41             position = (int)(Math.random() * 8);
 42             array[i][position] = 1;
 43         }
 44     }
 45
 46     //clear the chessboard
 47     public static void clearChessboard(int[][] array)
 48     {
 49         for(int i = 0; i < 8; i++)
 50             for(int j = 0; j < 8; j++)
 51                 array[i][j] = 0;
 52     }
 53
 54     //judge if there is only one chess in a row
 55     public static boolean judgeRow(int[][] array, int x, int y)
 56     {
 57         for(int i = y - 1; i >= 0; i--)
 58             if(array[x][i] == 1)
 59                 return false;
 60         for(int i = y + 1; i < 8; i++)
 61             if(array[x][i] == 1)
 62                 return false;
 63         return true;
 64     }
 65
 66     //judge if there is only one chess in a column
 67     public static boolean judgeColumn(int[][] array, int x, int y)
 68     {
 69         for(int i = x - 1; i >= 0; i--)
 70             if(array[i][y] == 1)
 71                 return false;
 72         for(int i = x + 1; i < 8; i++)
 73             if(array[i][y] == 1)
 74                 return false;
 75         return true;
 76     }
 77
 78     //judge if there is only one chess in the lean from left-top to right-bottom
 79     public static boolean judgeLeanTopToBottom(int[][] array, int x, int y)
 80     {
 81         for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--)
 82             if(array[i][j] == 1)
 83                 return false;
 84         for(int i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++)
 85             if(array[i][j] == 1)
 86                 return false;
 87         return true;
 88     }
 89
 90     //judge if there is only one chess in the lean from left-bottom to right-top
 91     public static boolean judgeLeanBottomToTop(int[][] array, int x, int y)
 92     {
 93         for(int i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--)
 94             if(array[i][j] == 1)
 95                 return false;
 96         for(int i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++)
 97             if(array[i][j] == 1)
 98                 return false;
 99         return true;
100     }
101
102     //judge if all the queens are put correctly
103     public static boolean judgeCorrect(int[][] array)
104     {
105         int[] putX = new int[8];
106         int[] putY = new int[8];
107
108         for(int i = 0; i < 8; i++)
109             for(int j = 0; j < 8; j++)
110                 if(array[i][j] == 1)
111                 {
112                     putX[i] = i;
113                     putY[i] = j;
114                     break;
115                 }
116
117         for(int i = 0; i < 8; i++)
118         {
119             if(!(judgeRow(array, putX[i], putY[i]) && judgeColumn(array, putX[i], putY[i])
120                 && judgeLeanTopToBottom(array, putX[i], putY[i]) && judgeLeanBottomToTop(array, putX[i], putY[i])))
121                 return false;
122         }
123         return true;
124     }
125
126     //convert the 2D chessboard in a 1D array
127     public static int[] convert(int[][] array)
128     {
129         int[] oneDQueen = new int[8];
130         for(int i = 0; i < 8; i++)
131             for(int j = 0; j < 8; j++)
132             {
133                 if(array[i][j] == 1)
134                     oneDQueen[i] = j;
135             }
136         return oneDQueen;
137     }
138
139     //judge if the answer has been found
140     public static boolean contain(int[][] largeArray, int[] littleArray)
141     {
142         for(int[] array: largeArray)
143             if(Arrays.equals(array, littleArray))
144                 return true;
145         return false;
146     }
147
148     //put the answer in the answer-save array
149     public static void putIn(int[][] largeArray, int[] littleArray)
150     {
151         int signI = 0;
152         for(int i = 0; i < 92; i++)
153             for(int j = 0; j < 7; j++)
154                 if(largeArray[i][j] == 0 && largeArray[i][j + 1] == 0)
155                 {
156                     signI = i;
157                     putIn(largeArray, littleArray, signI);
158                     return;
159                 }
160     }
161
162     public static void putIn(int[][] largeArray, int[] littleArray, int sign)
163     {
164         for(int i = 0; i < 8; i++)
165             largeArray[sign][i] = littleArray[i];
166     }
167
168     //judge if the array is full
169     public static boolean isFull(int[][] largeArray)
170     {
171         for(int j = 0; j < 8; j++)
172             if(largeArray[91][j] != 0)
173                 return true;
174         return false;
175     }
176
177     public static void drawChessboard(int[][] array)
178     {
179         for(int i = 0; i < 8; i++)
180         {
181             for(int j = 0; j < 8; j++)
182             {
183                 if(array[i][j] == 1)
184                     System.out.print("Q");
185                 else
186                     System.out.print("-");
187             }
188             System.out.println();
189         }
190     }
191 }
时间: 2024-07-28 17:04:09

HW6.22的相关文章

Up to 8% free bonus for runescape 2007 gp on Rsorder as july best gift&Enjoy Telos During 7.1-7.22

Now, a small band of freedom fighters struggle to end the osrs gold  long, dark night of Daein's oppression. The big blog news of the day is that Vox Media has acquired Curbed Network. As an amulet you should be wearing an amulet of glory and if you

2016/2/22 三级导航

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="

22.Android 十分方便的滑动标签页

22.Android 十分方便的滑动标签页 Android 十分方便的滑动标签页 前言 EasySlidingTabs属性 EasySlidingTabs布局 FragmentPagerAdapter EasySlidingTabs设置Tab背景 Github传送门 效果图 前言 其实滑动标签页是很常见的,网上搜也是一大堆.但是好用.简单.少bug.可扩展的库实在不多.很多想在做滑动标签页的时候也是经常想到各种不靠谱的库,虽然不难,但是容易坑自己. 原三星底层App大神JiangEcho提供技术

Solr4.8.0源码分析(22)之 SolrCloud的Recovery策略(三)

Solr4.8.0源码分析(22)之 SolrCloud的Recovery策略(三) 本文是SolrCloud的Recovery策略系列的第三篇文章,前面两篇主要介绍了Recovery的总体流程,以及PeerSync策略.本文以及后续的文章将重点介绍Replication策略.Replication策略不但可以在SolrCloud中起到leader到replica的数据同步,也可以在用多个单独的Solr来实现主从同步.本文先介绍在SolrCloud的leader到replica的数据同步,下一篇

2014年4月23日 10:22:08

step 1 : 做tcp网络编程,要解析一批批的数据,可是数据是通过Socket连接的InputStream一次次读取的,读取到的不是需要转换的对象,而是要直接根据字节流和协议来生成自己的数据对象. 按照之前的编程思维,总是请求然后响应,当然Socket也是请求和响应,不过与单纯的请求响应是不同的. 这里Socket连接往往是要保持住的,也就是长连接,然后设置一个缓冲区,网络流不断的追加到缓冲区.然后后台去解析缓冲区的字节流. http://cuisuqiang.iteye.com/blog/

22岁的这一年

时间流逝,昨日的22仿佛就像幻想般消失,再也回不去,只留下这记述的文字和慢慢模糊不清的记忆,给予追悔的冲动,奋发的思想.   三年前到佛山,为了那崇拜已久的神奇,为了那心中充满的兴趣踏上学习路,那时间可谓充实,真正把时间安排的井井有条,白天要工作,晚上要学习,要敲代码,有付出有收获,苦过,累过..人生就是这样一点一点起步的,没有完美,途中总有跌倒.失败.成功的时候, 往往在跌倒.失败的时候才有刻骨铭心的教诲,成功只是证明自己在失败中真正学到的东西,活着总要学会不断的失败,从失败中学习成长;在某一

横向受荷桩的设计软件 Oasys Alp 19.2.0.22

Oasys Alp 19.2.0.22 1CD 分析横向受力桩的土-结构相互作用的简易方法 当谈到横向受荷桩的设计软件,ALP使事情简单.这种横向受力桩分析软件模型的土壤剪切破坏和非线性的土壤行为,计算挠度下降的桩一起弯矩和剪力桩内. 选择选项,以适应任何打桩工程 桩的位移和位移 Acme CAD Converter 2016 8.7.5.1456 Portable 1CD  Bentley HAMMER CONNECT Edition 10.00.00.49 1CD  Bentley Wate

益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18

热增益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18 Andrey.Shirshov.Shprotification.v6.8.15.22 Andrey.Shirshov.Heat.Balance.v6.12.27.36 Andrey.Shirshov.Cold.Balance.v2.6.14.18   "现代ASHRAE标准参考数据用于从人.设备.确定热输入的人工照明,半透明的击剑系数(

[051] 微信公众平台开发教程第22篇-如何保证access_token长期有效

为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等,开发者在调用这些接口时,都需要传入一个相同的参数access_token,它是公众账号的全局唯一票据,它是接口访问凭证. access_token的有效期是7200秒(两小时),在有效期内,可以一直使用,只有当access_token过期时,才需要再次调用接口获取access_token.在理想情况下,一个7x24小时运行的系统,每天只需