八皇后题解
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 int vis[3][40]; 7 int sum = 0; 8 int n; 9 int ans[20]; 10 void queue(int x) 11 { 12 if (x > n) 13 { 14 sum++; 15 if (sum > 3) return; 16 else 17 { 18 for (int i = 1; i <= n; i++) 19 { 20 cout << ans[i] << " "; 21 } 22 cout << endl; 23 return; 24 } 25 } 26 for (int i = 1; i <= n; i++) 27 { //0表示列,1表示右上到左下斜线,2表示左上到右下斜线; 28 if (vis[0][i] == 0 && vis[1][x + i] == 0 && vis[2][x - i + n]==0) 29 { 30 ans[x] = i; 31 vis[0][i] = 1; 32 vis[1][x + i] = 1; 33 vis[2][x - i + n] = 1; 34 queue(x + 1); 35 vis[0][i] = 0; 36 vis[1][x + i] = 0; 37 vis[2][x - i + n] = 0; 38 } 39 } 40 41 } 42 int main() 43 { 44 cin >> n; 45 queue(1); 46 cout << sum << endl; 47 return 0; 48 }