leetcode 51|52. N-Queens 1|2

来源:互联网 发布:软妹日系滤镜软件 编辑:程序博客网 时间:2024/06/04 01:11

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.."]]

class Solution {public:    string getstring(int n, int i)    {        string m1 = "";        n = n - i - 1;        while (i > 0)        {            m1 = m1 + ".";            i--;        }        m1 = m1 + "Q";        while (n > 0)        {            m1 = m1 + ".";            n--;        }        return m1;    }        void solve(int n, int pos,                  // pos记录当前的行数               vector<vector<string>> &result,  //返回的结果               vector<string> guocheng,         //每一种结果               vector<int> count)               // count[i] 记录 i 这一列有没有被占    {        if (pos == n)//已经被填满了        {            result.push_back(guocheng);            return;        }                string m1;        int flag;        for (int i = 0; i < n; i++)  //第pos行的第i位能不能是Q        {            m1 = getstring(n, i);            if (pos == 0) //如果是第一行,那就随便填入,然后继续            {               guocheng.push_back(m1);               count[i] = 1;               solve(n, pos+1, result, guocheng, count);               count[i] = 0;               guocheng.pop_back();            }            else//不是第一行            {                if (count[i] == 1)//这列被占了                    continue;                else                {                    flag = 0;                    for (int k = 1; k <= pos; k++) //检查斜行或斜列                    {                        if ((guocheng[pos-k][i-k]=='Q' && i-k>=0) ||  (guocheng[pos-k][i+k]=='Q' && i+k<9))                        {                            flag = 1;                            break;                        }                     }                    if(flag == 1) //flag==1说明这一个斜行或斜列已经有了                        continue;                    guocheng.push_back(m1);                    count[i] = 1;                    solve(n, pos + 1, result, guocheng, count);                    count[i] = 0;                    guocheng.pop_back();                }            }        }    }        vector<vector<string>> solveNQueens(int n)     {        vector<vector<string>> result;        vector<string> guocheng;                if (n == 1)        {            guocheng.push_back("Q");            result.push_back(guocheng);            return result;        }        else if(n < 4)            return result;                 vector<int> count(n,0);        solve(n, 0, result, guocheng, count);        return result;    }};

52. N-Queens II

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

class Solution {public:    int all;    string getstring(int n,int i)    {        string m1="";        n=n-i-1;        while(i>0)        {            m1=m1+".";            i--;        }        m1=m1+"Q";        while(n>0)        {            m1=m1+".";            n--;        }        return m1;    }        void solve(int n, int pos, vector<string> guocheng, vector<int> count)    {        if (pos == n)//已经被填满了        {            all++;            return;        }                string m1;        int flag;        for (int i = 0; i < n; i++)        {            m1 = getstring(n,i);                       if (pos == 0)//如果是第一行            {                guocheng.push_back(m1);                count[i]=1;                solve(n,pos+1,guocheng,count);                count[i]=0;                guocheng.pop_back();            }            else//不是第一行            {                if(count[i]==1)//这列被占了                    continue;                else                {                    flag=0;                    for(int k=1;k<=pos;k++)                    {                        if((guocheng[pos-k][i-k]=='Q' && i-k>=0) ||  (guocheng[pos-k][i+k]=='Q' && i+k<9))                        {                            flag=1;                            break;                        }                     }                    if(flag==1)                        continue;                                guocheng.push_back(m1);                    count[i]=1;                    solve(n,pos+1,guocheng,count);                    count[i]=0;                    guocheng.pop_back();                }            }        }            }        int totalNQueens(int n)    {        vector<string> guocheng;        if(n==1)            return 1;        else if(n<4)            return 0;        vector<int> count(n,0);        all = 0;        solve(n, 0, guocheng, count);        return all;    }};




原创粉丝点击