HW6.20

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于HW6.20的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数