Eight queens question 1, Empress
Background: The eight queens question is a question based on chess: how can we place eight queens on the 8x8 chess board, so that no queen can directly eat other queens. To achieve thisAny two queens cannot be in the same horizontal, vertical, or diagonal line.
The solution provided by the following code should be the easiest way to understand. This problem can also be solved using backtracking and recursion. Theoretically, the efficiency is much better than this method.
1 #include<stdio.h> 2 3 int chess[8][8]; 4 int count = 0; 5 6 void display(){ 7 int row,col; 8 for(row = 0; row < 8; row++){ 9 for(col = 0; col < 8; col++){ 10 if(chess[row][col] == 1){ 11 printf("%c ",45); 12 } 13 else if(chess[row][col] == 2){ 14 putchar(1); 15 printf("%c",32); 16 17 } 18 19 } 20 printf("\n"); 21 } 22 getch(); 23 } 24 void setLimitsArea(int row,int col){ 25 int i,j; 26 for(j = 0; j < 8; j++) 27 if(col != j) 28 chess[row][j] = 1; 29 for(i = 0; i < 8; i++) 30 if(row != i) 31 chess[i][col] = 1; 32 for(i = 1; row - i >= 0 && col - i >= 0; i++) 33 chess[row - i][col - i] = 1; 34 for(i = row + 1,j = col + 1; i < 8 && j < 8; i++,j++) 35 chess[i][j] = 1; 36 for(i = 1; row - i >= 0 && col + i < 8; i++) 37 chess[row - i][col + i] = 1; 38 for(i = row + 1,j = col - 1; i < 8 && j >=0; i++,j--) 39 chess[i][j] = 1; 40 } 41 void eightQueensProblem(){ 42 int row1,row2,row3,row4,row5,row6,row7,row8; 43 int i, j; 44 for(row1 = 0;row1 < 8;row1++){ 45 for(row2 = 0;row2 < 8;row2++){ 46 for(row3 = 0;row3 < 8;row3++){ 47 for(row4 = 0;row4 < 8;row4++){ 48 for(row5 = 0;row5 < 8;row5++){ 49 for(row6 = 0;row6 < 8;row6++){ 50 for(row7 = 0;row7 < 8;row7++){ 51 for(row8 = 0;row8 < 8;row8++){ 52 for(i = 0;i < 8;i++) 53 for(j = 0;j < 8;j++) 54 chess[i][j] = 0; 55 chess[0][row1] = 2; 56 setLimitsArea(0,row1); 57 if(chess[1][row2] == 0){ 58 chess[1][row2] = 2; 59 setLimitsArea(1,row2); 60 if(chess[2][row3] == 0){ 61 chess[2][row3] = 2; 62 setLimitsArea(2,row3); 63 if(chess[3][row4] == 0){ 64 chess[3][row4] = 2; 65 setLimitsArea(3,row4); 66 if(chess[4][row5] == 0){ 67 chess[4][row5] = 2; 68 setLimitsArea(4,row5); 69 if(chess[5][row6] == 0){ 70 chess[5][row6] = 2; 71 setLimitsArea(5,row6); 72 if(chess[6][row7] == 0){ 73 chess[6][row7] = 2; 74 setLimitsArea(6,row7); 75 if(chess[7][row8] == 0){ 76 chess[7][row8] = 2; 77 count = count + 1; 78 printf(" NO %d Method \n",count); 79 display(); 80 } 81 } 82 } 83 } 84 } 85 } 86 } 87 } 88 } 89 } 90 } 91 } 92 } 93 } 94 } 95 } 96 97 int main(){ 98 int row = 0,col = 0; 99 for(row = 0; row < 8; row++)100 for(col = 0; col < 8; col++)101 chess[row][col] = 0;102 eightQueensProblem();103 return 0;104 }
Code Overview: setLimitArea is used when the Queen is placed in chess [row] [col] (the value of this element is set to 2 ). Set the row, column, main diagonal line, and diagonal line of the element to 1 (the original value of the entire array element is 0 ), it indicates that future queens cannot be placed here. The eightQueensProblem function determines each placement method of the eight queens (one row is placed, and each row tries from 0 to 7. Outputs the matching results. Dispaly is only a print function, but it will indicate that 2 of the Queen is displayed as a smiling face symbol, and 1 is represented as a line.