标签:
1 #include <stdio.h> 2 3 #define QUEEN_N (8) 4 5 int queen[QUEEN_N][QUEEN_N] = { 6 {0, 0, 0, 0, 0, 0, 0, 0}, 7 {0, 0, 0, 0, 0, 0, 0, 0}, 8 {0, 0, 0, 0, 0, 0, 0, 0}, 9 {0, 0, 0, 0, 0, 0, 0, 0}, 10 {0, 0, 0, 0, 0, 0, 0, 0}, 11 {0, 0, 0, 0, 0, 0, 0, 0}, 12 {0, 0, 0, 0, 0, 0, 0, 0}, 13 {0, 0, 0, 0, 0, 0, 0, 0}, 14 }; 15 16 int queen_check(int x, int y) 17 { 18 int i; 19 20 for(i = 0; i < QUEEN_N; i++) 21 { 22 if(queen[i][y] == 1 && i != x) // 检测列 23 return 0; 24 if(queen[x][i] == 1 && i != y) // 检测行 25 return 0; 26 } 27 28 // 检测对角线,x正向方向 29 for(i = x+1; i < QUEEN_N; i++) 30 { 31 if(y + i - x < QUEEN_N) 32 if(queen[i][y+i-x] == 1) 33 return 0; 34 if(y - (i - x) >= 0) 35 if(queen[i][y-i+x] == 1) 36 return 0; 37 } 38 // 检测对角线,x反向方向 39 for(i = x-1; i >=0; i--) 40 { 41 if(y + (x-i) < QUEEN_N) 42 if(queen[i][y+x-i] == 1) 43 return 0; 44 if(y - (x-i) >= 0) 45 if(queen[i][y-x+i] == 1) 46 return 0; 47 } 48 return 1; 49 } 50 51 void queen_print(int queen[QUEEN_N][QUEEN_N]) 52 { 53 int i, j; 54 55 for(i = 0; i < QUEEN_N; i++) 56 { 57 for(j = 0; j < QUEEN_N; j++) 58 { 59 if(queen[i][j] == 1) 60 { 61 printf("O "); 62 } 63 else 64 { 65 printf("x "); 66 } 67 } 68 printf("\n"); 69 } 70 } 71 72 // 输出所有棋盘 73 void queen_fill(int n) 74 { 75 int i; 76 static int iCount = 0; 77 if(n >= QUEEN_N) 78 { 79 printf("queen_print <%d>\n", iCount++); 80 queen_print(queen); 81 } 82 else 83 { 84 for(i = 0; i < QUEEN_N; i++) 85 { 86 queen[n][i] = 1; 87 if(queen_check(n, i) == 1) 88 { 89 queen_fill(n+1); 90 } 91 queen[n][i] = 0; 92 } 93 } 94 } 95 // 输出第1个棋盘 96 int queen_fill_1(int n) 97 { 98 int i; 99 if(n >= QUEEN_N) 100 { 101 queen_print(queen); 102 return 1; 103 } 104 else 105 { 106 for(i = 0; i < QUEEN_N; i++) 107 { 108 queen[n][i] = 1; 109 if(queen_check(n, i) == 1) 110 { 111 if(queen_fill_1(n+1) == 1) 112 return 1; 113 } 114 queen[n][i] = 0; 115 } 116 } 117 return 0; 118 } 119 120 // 对于原始棋盘,判断是否能输出满足的棋盘 121 int queen_fill_x(int n) 122 { 123 int i; 124 if(n >= QUEEN_N) 125 { 126 queen_print(queen); 127 return 1; 128 } 129 else 130 { 131 for(i = 0; i < QUEEN_N; i++) 132 { 133 if(queen[n][i] == 0) 134 { 135 queen[n][i] = 1; 136 if(queen_check(n, i) == 1) 137 { 138 if(queen_fill_x(n+1) == 1) 139 return 1; 140 } 141 queen[n][i] = 0; 142 } 143 else 144 { 145 if(queen_check(n, i) == 1) 146 { 147 if(queen_fill_x(n+1) == 1) 148 return 1; 149 } 150 } 151 } 152 } 153 return 0; 154 } 155 156 int main(void) 157 { 158 int iRet; 159 printf("init queen:\n"); 160 queen_print(queen); 161 162 printf("queen_fill_x:\n"); 163 iRet = queen_fill_x(0); 164 if(iRet != 1) 165 { 166 printf("xxx err\n"); 167 } 168 169 printf("queen_fill_1:\n"); 170 queen_fill_1(0); 171 172 printf("queen_fill_all:\n"); 173 queen_fill(0); 174 175 return 0; 176 }
标签:
原文地址:http://www.cnblogs.com/utank/p/4330244.html