• 数据结构 八皇后问题 (暴力,回溯)


    Description

    设计程序完成如下要求:在8×8的国际象棋棋盘上,放置8个皇后,使得这8个棋子不能互相被对方吃掉。要求:依次输出各种成功的放置方法。(按照字典序输出)

    Input

    输入包含多组数据,第一行为数据组数,以后每行一个整数k,代表需要输出的字典序第k大的摆放方法。

    Output

    每组数据一行,依次输出第i列的皇后所在行数。

    Sample Input

    1
    1

    Sample Output

    15863724

    HINT

    Append Code

    析:这个题同样是暴力回溯,我们可以提前把表都打出来,set来排一下序,最后输出就好,在回溯时,要同时记录每一行,每一列和两个对角线,

    可以用一个两维数组vis来记录。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define debug puts("+++++")
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
     
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 5e4 + 5;
    const LL mod = 1e9 + 7;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
    inline int lcm(int a, int b){  return a * b / gcd(a, b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    bool vis[3][200];
    char s[10];
    vector<string> ans;
     
    void dfs(int cur){
        if(cur == n){ ans.push_back(s); return ;  }
        for(int i = 0; i < n; ++i){
            if(!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+n]){
                s[cur] = i + '1';
                vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 1;
                dfs(cur+1);
                vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 0;
            }
        }
    }
     
    int main(){
        n = 8;
        memset(s, 0, sizeof s);
        memset(vis, 0, sizeof vis);
        dfs(0);
        sort(ans.begin(), ans.end());
        int T;   cin >> T;
        while(T--){
            scanf("%d", &n);
            cout << ans[n-1] << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    php json_encode JSON_UNESCAPED_UNICODE
    ubuntu 添加多个ssh公钥和私钥
    如何自动化新增配置文件呢?
    git配置ssh公钥
    项目PHP新知识点
    mysql 数据库复制表 create table city1 like city;
    .NET 泛型集合数据写CSV文件
    .NET C# 泛型队列
    逆向地理编码--根据地址搜索定位,点击地图、获取经纬度信息
    正向地理编码-根据输入地址获取经纬度
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5990729.html
Copyright © 2020-2023  润新知