LV 八皇后

来源:互联网 发布:matlab for mac 2016 编辑:程序博客网 时间:2024/03/29 16:16
#include <iostream>#include <vector>#include <stack>#include<cstdlib>#define queenCount 16#define maxTryCount 1000000#define  intMaxValue 2147483647#define  exeCount  1000//每个stepVegas执行exeCount次对比using namespace std;int tryPosition[queenCount+1];bool contains(vector<int> temp,int val)    {for(int i=0;i<temp.size();i++)        {if(val==temp[i])            {return true;            }        }     return false;    }void backTrace(int k,vector<int> col,vector<int> diag45,vector<int> diag135,int &success,int &count)    {stack<int> columnAssembly;    int i=k;//k belongs to [1,queenCount]    int j=1;    while(i<=queenCount)        {        while(j<=queenCount)            {if((!contains(col,j))&&(!contains(diag45,j-i))&&(!contains(diag135,j+i)))                {                columnAssembly.push(j);                tryPosition[i]=j;                col.push_back(j);                diag45.push_back(j-i);                diag135.push_back(j+i);                i++;                j=1;                break;                }             j++;             count++;            }         if(j>queenCount)            {            i--;            if(i<k)//can't find positions to lay queens in security.                {success=0;                return;                }             col.pop_back();             diag45.pop_back();             diag135.pop_back();             j=columnAssembly.top()+1;             columnAssembly.pop();            }        }        success=1;    }void QueensLv(int &success,int &count,int stepVegas)    {vector<int> col;     vector<int> diag45;     vector<int> diag135;     int k=0;// the number of row     int nb;//the number of position that queen can be laid.        do{        nb=0;        int j=1;        for(int i=1;i<=queenCount;i++)//i is column            {if((!contains(col,i))&&(!contains(diag45,i-k-1))&&(!contains(diag135,i+k+1)))//                {nb=nb+1;                 if(rand()%nb+1==1)                    j=i;                }             count++;            }        if(nb>0)            {k=k+1;            tryPosition[k]=j;            col.push_back(j);            diag45.push_back(j-k);            diag135.push_back(j+k);            }        }while((0!=nb)&&(stepVegas!=k));    if(nb>0)        backTrace(k+1,col,diag45,diag135,success,count);    else        success=0;    }int main(){srand(time(0)); int stepVegasExeCount[queenCount+1]; for(int stepVegas=1;stepVegas<=queenCount;stepVegas++)//Don't include the case that pure backtrace,due to the restriction of implementation of queenslv.     {stepVegasExeCount[stepVegas]=0;//When stepVegas's value is queenCount,it represents the case of pure LV.      for(int i=0;i<exeCount;i++)        { int success=0;          int queenLvExeCount=0;         while(1!=success&&maxTryCount!=queenLvExeCount)//if stepVegas is too large,the probability of failure will be large            {            QueensLv(success,stepVegasExeCount[stepVegas],stepVegas);            queenLvExeCount++;            }//         if(maxTryCount==queenLvExeCount)//            {stepVegasExeCount[stepVegas]=intMaxValue;////            }        }        cout<<stepVegasExeCount[stepVegas]<<" ";     } cout<<endl; int minStepVegasExeCount=intMaxValue; int bestStepVegas=-1; for(int stepVegas=1;stepVegas<=queenCount;stepVegas++)    {if(stepVegasExeCount[stepVegas]<minStepVegasExeCount)        {minStepVegasExeCount=stepVegasExeCount[stepVegas];        bestStepVegas=stepVegas;        }    } cout<<"bestStepVegas:"<<bestStepVegas<<endl;// int count=0;// int queenLvExeCount=0;// int success=0;// while(1!=success&&maxTryCount!=queenLvExeCount)//if stepVegas is too large,the probability of failure will be large//        {//        QueensLv(success,count,15);//        queenLvExeCount++;//        }// if(maxTryCount!=queenLvExeCount)// {//     cout<<queenLvExeCount<<endl;//     for(int i=1;i<=queenCount;i++)//        {//        for(int j=1;j<=queenCount;j++)//             {if(j==tryPosition[i])//                cout<<"*"<<" ";//              else//                cout<<"-"<<" ";//             }//         cout<<endl;//        }// }}
0 0