首页 > 代码库 > HW6.20
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 else123 System.out.print("-");124 }125 System.out.println();126 }127 }128 }
HW6.20
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。