HW6.20

  1 public class Solution
  2 {
  3     public static void main(String[] args)
  4     {
  5         int[][] chessboard = new int[8][8];
  6
  7         while(true)
  8         {
  9             putQueens(chessboard);
 10
 11             if(judgeCorrect(chessboard) == true)
 12             {
 13                 drawChessboard(chessboard);
 14                 break;
 15             }
 16             else
 17                 clearChessboard(chessboard);
 18         }
 19     }
 20
 21
 22     //put 8 queens on the chessboard randomly
 23     public static void putQueens(int[][] array)
 24     {
 25         int position;
 26
 27         for(int i = 0; i < 8; i++)
 28         {
 29             position = (int)(Math.random() * 8);
 30             array[i][position] = 1;
 31         }
 32     }
 33
 34     //clear the chessboard
 35     public static void clearChessboard(int[][] array)
 36     {
 37         for(int i = 0; i < 8; i++)
 38             for(int j = 0; j < 8; j++)
 39                 array[i][j] = 0;
 40     }
 41
 42     //judge if there is only one chess in a row
 43     public static boolean judgeRow(int[][] array, int x, int y)
 44     {
 45         for(int i = y - 1; i >= 0; i--)
 46             if(array[x][i] == 1)
 47                 return false;
 48         for(int i = y + 1; i < 8; i++)
 49             if(array[x][i] == 1)
 50                 return false;
 51         return true;
 52     }
 53
 54     //judge if there is only one chess in a column
 55     public static boolean judgeColumn(int[][] array, int x, int y)
 56     {
 57         for(int i = x - 1; i >= 0; i--)
 58             if(array[i][y] == 1)
 59                 return false;
 60         for(int i = x + 1; i < 8; i++)
 61             if(array[i][y] == 1)
 62                 return false;
 63         return true;
 64     }
 65
 66     //judge if there is only one chess in the lean from left-top to right-bottom
 67     public static boolean judgeLeanTopToBottom(int[][] array, int x, int y)
 68     {
 69         for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--)
 70             if(array[i][j] == 1)
 71                 return false;
 72         for(int i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++)
 73             if(array[i][j] == 1)
 74                 return false;
 75         return true;
 76     }
 77
 78     //judge if there is only one chess in the lean from left-bottom to right-top
 79     public static boolean judgeLeanBottomToTop(int[][] array, int x, int y)
 80     {
 81         for(int i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--)
 82             if(array[i][j] == 1)
 83                 return false;
 84         for(int i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++)
 85             if(array[i][j] == 1)
 86                 return false;
 87         return true;
 88     }
 89
 90     //judge if all the queens are put correctly
 91     public static boolean judgeCorrect(int[][] array)
 92     {
 93         int[] putX = new int[8];
 94         int[] putY = new int[8];
 95
 96         for(int i = 0; i < 8; i++)
 97             for(int j = 0; j < 8; j++)
 98                 if(array[i][j] == 1)
 99                 {
100                     putX[i] = i;
101                     putY[i] = j;
102                     break;
103                 }
104
105         for(int i = 0; i < 8; i++)
106         {
107             if(!(judgeRow(array, putX[i], putY[i]) && judgeColumn(array, putX[i], putY[i])
108                 && judgeLeanTopToBottom(array, putX[i], putY[i]) && judgeLeanBottomToTop(array, putX[i], putY[i])))
109                 return false;
110         }
111         return true;
112     }
113
114     public static void drawChessboard(int[][] array)
115     {
116         for(int i = 0; i < 8; i++)
117         {
118             for(int j = 0; j < 8; j++)
119             {
120                 if(array[i][j] == 1)
121                     System.out.print("Q");
122                 else
123                     System.out.print("-");
124             }
125             System.out.println();
126         }
127     }
128 }
时间: 2024-08-10 18:27:41

HW6.20的相关文章

u近一年很变态个v分

http://ypk.39.net/search/all?k=%20%CA%AF%CA%A8%B4%DF%C7%E9%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%BA%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7%A8L http://ypk.39.net/search/all?k=%A1%FD%CF%C9%D3%CE%B4%DF%C7%E9%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%

20、oracle用户管理恢复

下面会一一讲解控制文件.redo文件及非归档模式.归档模式数据文件丢失的情况下,如何恢复数据? (1)控制文件(controlfile)丢失 在做恢复实验之前,先备份好数据. 案例1.模拟一个控制文件丢失 select name from v$controlfile; SQL> select name from v$controlfile; NAME -------------------------------------------------------------------------

20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)

扩展: select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html 20.5 Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 1. 创建if1.sh测试脚本: [[email protected] ~]# vi if1.sh a=5,如果a大于3,满足这个条件,显示ok 添加内容: #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi 2. 执行if1.sh脚本: [[e

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量

20.1 Shell脚本介绍 1. shell是一种脚本语言 aming_linux blog.lishiming.net 2. 可以使用逻辑判断.循环等语法 3. 可以自定义函数 4. shell是系统命令的集合 5. shell脚本可以实现自动化运维,能大大增加我们的运维效率 20.2 Shell脚本结构和执行 1. 开头(首行)需要加: #!/bin/bash 2. 以#开头的行作为解释说明: 3. 脚本的名字以.sh结尾,用于区分这是一个shell脚本 4. 执行.sh脚本方法有两种:

20.27分发系统介绍;20.28expect脚本远程登录;20.29expect脚本远程执行命令;20.30expect脚本传递参数

20.27 分发系统介绍 shell项目-分发系统-expect 20.28 expect脚本远程登录 1. 安装expect [[email protected] ~]# yum install -y expect 自动远程登录 2. 创建配置1.expect脚本(远程登录) [[email protected] ~]# vim 1.expect 添加内容(自动远程登录hao2机器并执行命令) #! /usr/bin/expect set host "192.168.211.129"

成功产品人必备的20项特质

怎样成为一个好的产品经理?本文通过观察数百位产品经理,总结了产品人必备的20项特质,一起开看看吧,希望对大家有所帮助. 01.从问"为什么"开始 你必须能清晰的回答为什么会有人用你的产品,你的产品能为他们解决生活上的什么难题等这样的问题.你要关注用户写的评论.一旦确定了这个产品的定位和愿景,就一定要围绕这个定位和愿景开发产品.正如亚马逊CEO杰夫.贝佐斯所说:"Be stubborn on the vision, flexible on thedetails."(坚

2017-7-27-关键20小时,快速习得任何技能

2017-7-27-关键20小时,快速习得任何技能 thinking 总之,就是在有限时间内保持专注,和不断练习 [email protected] 2017-7-27

你必须知道的改变中国人工智能命运的20个人

近日,福布斯发表一篇名为<20个推动人工智能改革的科技领导者>的署名文章,介绍了中国顶尖科技公司中的20位致力于人工智能的重要人物,并认为在人工智能领域中国正在挑战美国的领导地位.在福布斯列出的20位重要人物中,有10位出自百度,其中7位如今都在百度担任人工智能领域的重要职务. 这7位人工智能重要人物中,最受关注的是今年年初加入百度,担任百度集团总裁兼COO的陆奇,此前陆奇曾服务微软,担任微软应用与服务部门执行副总裁,曾经是美国科技行业中担任最高管理职位的华人,加入百度后负责领导公司的人工智能

2017考研英语:给作文模板增色的20句谚语

2017考研英语:给作文模板增色的20句谚语 2016-12-14 14:37:43 来源:新东方在线考研资料下载 靠谱名校专业课 最新资讯:2018考研复习要重点关注的6个月份 2018推免生考研必须了解的三件事 考研关注:过来人谈读研后的6大收获 英语单词背诵3大方法 18政治史纲各章节必背考点 精华推荐:名校学长学姐一对一考研答疑 向TA提问 [限额抢课]复试精华直播 课程推荐:2018考研签约全程联报 [政治+英语] 2018考研英数签约全程班 考研冲刺复习时间不多,大家作文背的咋样,模