• N皇后问题


    #include <cstdlib>
    #include <iostream>

    using namespace std;

    bool place(int x[],int k);
    void queen(int n,int x[]);
    void Output(int n,int x[]);
    int main(int argc, char *argv[])
    {
        cout<<"请输入皇后的个数\n";
        int n;
        cin>>n;
        int x[n+1];
        x[0]=0;
        cout<<"解向量是----\n";
        queen(n,x);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    bool place(int x[],int k)
    {
        for(int i=1;i<k;i++)
            if((x[i]==x[k])||(abs(x[i]-x[k])==abs(i-k)))
                return 0;
        return 1;
    }
    void queen(int n,int x[])
    {
        int k=1;
        long num=0;
        x[1]=0;
        while(k>0)
            {
                x[k]+=1;
                while((x[k]<=n)&&(!place(x,k)))
                    x[k]+=1;
                if(x[k]<=n)
                    if(k==n)
                        {
                            num++;
                            Output(n,x);
                        }
                    else
                        x[++k]=0;
                else
                    x[k--]=0;
            }
        system("PAUSE");
        cout<<"一共有"<<num<<"种情况\n";
        return;
    }
    void Output(int n,int x[])
    {         
        cout<<"[";
        for(int i=1;i<n;i++)
            cout<<x[i]<<",";
        cout<<x[n]<<"]"<<endl;
        return;
    }

  • 相关阅读:
    94. Binary Tree Inorder Traversal
    101. Symmetric Tree
    38. Count and Say
    28. Implement strStr()
    实训团队心得(1)
    探索性测试入门
    LC.278. First Bad Version
    Search in Unknown Sized Sorted Array
    LC.88. Merge Sorted Array
    LC.283.Move Zeroes
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035146.html
Copyright © 2020-2023  润新知