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 }

 

转载于:https://www.cnblogs.com/wood-python/p/5812906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值