zoukankan      html  css  js  c++  java
  • 51. N-Queens

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

    Given an integer n, return all distinct solutions to the n-queens puzzle.

    Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

    For example,
    There exist two distinct solutions to the 4-queens puzzle:

    [
     [".Q..",  // Solution 1
      "...Q",
      "Q...",
      "..Q."],
    
     ["..Q.",  // Solution 2
      "Q...",
      "...Q",
      ".Q.."]
    ]
    
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/4/3 12:48:50
    File Name     :1.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    class Solution {
    public:
        void dfs(int n,int cur,vector<string>&v,vector<vector<string>>&u,int *row,int *col,int *zu,int *fu){
            if(cur==n){
                u.push_back(v);
                return ;
            }
            for(int j=0;j<n;j++){
                if(v[cur][j]=='.'&&(row[cur]==0)&&(col[j]==0)&&(zu[j-cur+n]==0)&&(fu[j+cur]==0)){
                    v[cur][j]='Q';
                    row[cur]=col[j]=zu[j-cur+n]=fu[j+cur]=1;
                    dfs(n,cur+1,v,u,row,col,zu,fu);
                     row[cur]=col[j]=zu[j-cur+n]=fu[j+cur]=0;
                    v[cur][j]='.';
                }
            }
            //  dfs(cur+1)
        }
        vector<vector<string>> solveNQueens(int n) {
            vector<string>v;
            vector<vector<string>>u;
            int zu[100]={0};
            int fu[100]={0};
            int col[100]={0};
            int row[100]={0};
            
            for(int i=0;i<n;i++){
                string s="";
                for(int j=0;j<n;j++)
                    s+=".";
                v.push_back(s);
            }
            dfs(n,0,v,u,row,col,zu,fu);
            return u;
        }
    };
    bool cmp(int a,int b){
        return a>b;
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        Solution a;
        vector<vector<string>>u=a.solveNQueens(4);
        for(int i=0;i<u.size();i++){
            for(int j=0;j<4;j++){
                cout<<u[i][j]<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    vim lua对齐indent无效
    C中的私有成员
    Lua 设置table为只读属性
    c语言结构体可以直接赋值
    Lua5.3 注册表 _G _ENV
    火狐浏览器调试ajax异步页面时报错NS_ERROR_UNEXPECTER
    ajax向后台请求数据,后台接收到数据并进行了处理,但前台就是调用error方法
    maven安装之后,或者升级之后遇到的问题:could not find or load main class org.codehaus.plexus.class.....
    jenkins执行shell命令,有时会提示“Command not found”
    shell 脚本替换文件中某个字符串
  • 原文地址:https://www.cnblogs.com/pk28/p/5349666.html
Copyright © 2011-2022 走看看