Codeup100000609 problems D: Getting recursive] [n queens problem (the original 8-queens problem) optimization of backtracking

1

Question D: [entry-recursive] n queens problem (the original 8-queens problem)
Time limit: 1 Sec Memory Limit: 128 MB
submit: 926 Resolution: 436
[submit] [state] [Discussion Board] [proposition man: External Import]
title description
play chess people are very clear: the queen can not eat another piece to the number of steps in the horizontal, vertical, diagonal. How eight queens on the chessboard (8 * 8 squares), so that they can not be eaten by anyone! This is the famous eight queens problem.

Enter
an integer n (1 <= n <= 10)

The output
of each output line corresponding to a scheme, all output lexicographical ordering scheme. Each column Queen sequentially outputs the program is located, adjacent two numbers separated by spaces. "! No solute" If a group of programs are not feasible, the output
Figure 1 five Queens
FIGS one
shown above is arranged in a corresponding 2,4,1,3,5; b are arranged corresponding 3,5,1,4,2

样例输入
4
样例输出
2 4 1 3
3 1 4 2

2 parsed

  • Title: 1 full ordering within a given range - in N, is obtained for each element arranged in each of the other elements are not arranged in horizontal, vertical, diagonal arrangement of the program
  • An idea: enumeration 1 ~ N full array, each include a complete set, then the program tests the legality of the current group arrangement (the arrangement of each element with other elements are not arranged in horizontal, vertical, diagonal on)
  • Two ideas: on the premise of one embodiment, to optimize the backtracking, in each set include each whole arrangement of elements when each element include, determines whether a conflict before and elements (each element arrangement the arrangement with other elements in the horizontal, vertical, diagonal), it will not conflict if the element into the set; if a conflict, directly next group include

3 reference code

  • A thought:
#include <cstdio>
#include <cstdlib>

const int MAXN = 100;
int N;
bool hashTable[MAXN]  = {false};
int P[MAXN];
int count = 0;

void DFS(int index){//朴素算法
    if(index == N + 1){
        bool flag = true;//flag为true,表示当前方案合法
        for (int i = 1; i <= N ; ++i)//遍历两个皇后,
        {
            for (int j = i + 1; j <= N; ++j)
            {
                //第四象限内,P[y]为纵坐标,i、j为横坐标
                if(abs(i - j) == abs(P[j] - P[i])){//如果在一条对角线上,也就是正方形相对的两个端点
                    flag = false;
                }
            }
        }

        if(flag == true) {
            count++;
            for (int i = 1; i <= N; ++i)
            {
                printf("%d", P[i]);
                if(i <= N - 1) printf(" ");
            }
            printf("\n");
        }
    }

    for (int i = 1; i <= N; ++i)//全排列
    {
        if(hashTable[i] == false){
            P[index] = i;
            hashTable[i] = true;
            DFS(index + 1);
            hashTable[i] = false;
        }
    }
}

int main(){
    scanf("%d", &N);
    DFS(1);
    if(count == 0) printf("no solute!\n");
    return 0;
}
  • Thinking two:
#include <cstdio>
#include <cstdlib>

const int MAXN = 100;
int N;
bool hashTable[MAXN]  = {false};
int P[MAXN];
int count = 0;

void DFS(int index){//朴素算法
    if(index == N + 1){
        count++;
            for (int i = 1; i <= N; ++i)
            {
                printf("%d", P[i]);
                if(i <= N - 1) printf(" ");
            }
            printf("\n");
            return;
        }


    for (int i = 1; i <= N; ++i)//第i行
    {
        if(hashTable[i] ==  false){
            bool flag = true;
            for (int pre = 1; pre < index; ++pre)
            {
                //第index列的皇后行号为i,第pre列的行号为P[pre]
                if(abs(index - pre) == abs(P[pre] - i)){
                    flag = false;
                    break;
                }
            }

            if(flag == true){
                P[index] = i;
                hashTable[i] = true;
                DFS(index + 1);
                hashTable[i] = false;
            }
        }
        
    }
}

int main(){
    scanf("%d", &N);
    DFS(1);
    if(count == 0) printf("no solute!\n");
    return 0;
}
Published 321 original articles · won praise 51 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_33375598/article/details/104059358