经典八皇后问题
- #include "iostream"
- using namespace std;
- #define N 8
- int queue[N];
- int count = 1;
-
- int getQueueK(int k)
- {
- if (k == N)
- {
- for (int m = 0; m< N; m++)
- {
- cout<<queue[m]<<"/t";
- }
- cout<<endl<<count++<<":------------------"<<endl;
- return N;
- }
- for (int i = 0; i < N; i++)
- {
- int flag = 1;
- for (int j = 0; j < k; j++)
- {
- if ((i - queue[j] == k - j) || (i == queue[j]) || (i - queue[j] == j- k))
- {
- flag = 0;
- break;
- }
- }
- if (flag == 1)
- {
- queue[k] = i;
- getQueueK(k+1);
- }
- }
- return -1;
- }
-
- int main()
- {
- getQueueK(0);
- return 0;
- }