N皇后问题,深度优先搜索(通俗易懂)

洛谷N皇后问题

问题解法

使用二维数组将棋盘存储下来,可以下棋的位置为0,有棋子的位置为1,不能走的位置为2
这是一个典型的深度优先搜索的问题。
第一步我们需要在棋盘的第一行上选择一个点,作为落子点,封锁和这点的同列同行对角线的位置,(可以将这些位置设置为2)
第二步,开始搜索下一行棋子的可行位置,如果还有就继续下一行,直到最后一行被添上。说明这是问题的一个解。
第三步,如果搜索下一行棋子的时候没有可行位置,就返回到上一行的位置,并在上一行的位置中,继续搜索可行位置。

完整代码

#include <iostream>
using namespace std;

int chess[13][13];
int book[13][13];
int ans = 0;
int ans_list[5][13];
int maxn = 10;

void feisuo(int tx,int ty){
    for(int i = 0;i < maxn; i++){
        chess[tx][i] = 2;
        chess[i][ty] = 2;
    }
    // 对角线也只能有1个棋子
    for(int i = 1;i < maxn; i++){
        if( tx + i < maxn && ty + i < maxn)
            chess[tx + i][ty + i] = 2;
        else
            break;
    }
    for(int i = 1;i < maxn; i++){
        if( tx - i >= 0 && ty - i >= 0)
            chess[tx - i][ty - i] = 2;
        else
            break;
    }
    for(int i = 1;i < maxn; i++){
        if( tx - i >= 0 && ty + i < maxn)
            chess[tx - i][ty + i] = 2;
        else
            break;
    }
    for(int i = 1;i < maxn; i++){
        if( tx + i < maxn && ty - i >= 0)
            chess[tx + i][ty - i] = 2;
        else
            break;
    }
}

void dfs(int n, int x){
    // 如果搜索到最后
    if(n==0){
        if(ans > 3){
            ans++;
            return;
        }
        for(int i = 0; i < maxn; i++){
            for(int j = 0; j < maxn; j++){
                if(chess[i][j] == 1){
                    ans_list[ans][i] = j;
                    break;
                }
            }
        }
        ans++;
        return;
    }
    for(int i = 0; i < maxn; i++){
        int tx = x + 1;
        int ty = i;
        // 判断是否越界
        if(tx < 0 || tx >= maxn ) continue;
        // 判断是否可以走
        if(chess[tx][ty] != 0) continue;
        // 备份棋盘
        int chessTemp[maxn][maxn];
        for(int i = 0; i < maxn; i++){
            for(int j = 0; j < maxn; j++){
                chessTemp[i][j] = chess[i][j];
            }
        }
        // 封锁棋盘
        feisuo(tx,ty);
        chess[tx][ty] = 1;
        // 进入下一层
        dfs(n-1,tx);

        // 还原棋盘
        for(int i = 0; i < maxn; i++){
            for(int j = 0; j < maxn; j++){
                chess[i][j] = chessTemp[i][j];
            }
        }
    }
    return;
}

int main()
{
    cin >> maxn;
    // 初始只搜索第一行
    for(int i = 0; i < maxn; i++){

        int chessTemp[maxn][maxn];
        for(int i = 0; i < maxn; i++){
            for(int j = 0; j < maxn; j++){
                chessTemp[i][j] = chess[i][j];
            }
        }
        feisuo(0,i);
        chess[0][i] = 1;
        dfs(maxn-1,0);

        // 还原棋盘
        for(int i = 0; i < maxn; i++){
            for(int j = 0; j < maxn; j++){
                chess[i][j] = chessTemp[i][j];
            }
        }
    }


    // 输出答案
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < maxn; j++){
            cout << ans_list[i][j]+1 << ' ';
        }
        cout << endl;
    }
    cout << ans << endl;
    return 0;
}
发布了35 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43497702/article/details/104158076