有个实验是n皇后问题。分析其复杂度
The main task of lab2 is solving n queen problem by using DFS, BFS, and Monte-Carlo. As I mentioned before, depth first search is perfect to this problem both in searching single and all solutions. DFS and BFS take the same time ( ) in finding all solutions, but BFS cost much more space to finish searching. This is the reason account for why DFS performs better.
Because Monte-Carlo is unpredictable, it is hard for us to measure its efficiency.
深度搜索n皇后
#include<iostream>
#include<iomanip>
#include<cmath>
#include<stack>
#include<time.h>
using namespace std;
static int count;
bool place(int i,int j,int* path)
{
int row;
for(row=0;row<i;row++)
{
if(abs(row-i)==abs(path[row]-j))
return false;
if(j==path[row])
return false;
}
return true;
}
struct pos
{
int row;
int col;
};
int find_col(int row,int col,int* path,int n)
{
int j;
for(j=col;j<n;j++)
{
if(place(row,j,path))
return j;
}
if(j==n)
return -1;
}
void stack_stimu(int n,int* path)
{
stack<struct pos> s;
int currow=0;
int flag=0;
while(true)
{
if(currow<n)
{
int col=find_col(currow,0,path,n);
if(col!=-1)
{
pos node;
node.row=currow;
node.col=col;
s.push(node);
path[currow]=col;
currow++;
}
else
flag=1;
}
else
{
for(int i=0;i<n;i++)
cout<<setw(5)<<left<<path[i];
cout<<endl;
count++;
flag=1;
}
if(flag==1)
{
while(!s.empty())
{
pos temp=s.top();
if(temp.col!=7)
{
int j=find_col(temp.row,temp.col+1,path,n);
if(j!=-1)
{
pos node;
node.row=temp.row;
node.col=j;
s.pop();
s.push(node);
path[temp.row]=j;
currow=temp.row+1;
flag=0;
break;
}
else
s.pop();
}
else
s.pop();
}
if(s.empty())
return;
}
}
}
int main()
{
clock_t start, end;
float duration;
cout<<"Queen Place Problem:"<<endl;
cout<<"Input the value of n"<<endl;
int n;
cout<<" n>";
cin>>n;
int* path=new int[n];
for(int i=0;i<n;i++)
path[i]=-1000;
start=clock();
stack_stimu(n,path);
end=clock();
duration=end-start;
cout<<"the count is: "<<count<<endl;
cout<<"the duration is: "<<duration<<endl;
getchar();
getchar();
return 0;
}
广度 搜索n皇后
#include<iostream>
#include<iomanip>
#include<cmath>
#include<stack>
#include<time.h>
using namespace std;
static int count;
bool place(int i,int j,int* path)
{
int row;
for(row=0;row<i;row++)
{
if(abs(row-i)==abs(path[row]-j))
return false;
if(j==path[row])
return false;
}
return true;
}
struct pos
{
int row;
int col;
};
int find_col(int row,int col,int* path,int n)
{
int j;
for(j=col;j<n;j++)
{
if(place(row,j,path))
return j;
}
if(j==n)
return -1;
}
void stack_stimu(int n,int* path)
{
stack<struct pos> s;
int currow=0;
int flag=0;
while(true)
{
if(currow<n)
{
int col=find_col(currow,0,path,n);
if(col!=-1)
{
pos node;
node.row=currow;
node.col=col;
s.push(node);
path[currow]=col;
currow++;
}
else
flag=1;
}
else
{
for(int i=0;i<n;i++)
cout<<setw(5)<<left<<path[i];
cout<<endl;
count++;
flag=1;
}
if(flag==1)
{
while(!s.empty())
{
pos temp=s.top();
if(temp.col!=7)
{
int j=find_col(temp.row,temp.col+1,path,n);
if(j!=-1)
{
pos node;
node.row=temp.row;
node.col=j;
s.pop();
s.push(node);
path[temp.row]=j;
currow=temp.row+1;
flag=0;
break;
}
else
s.pop();
}
else
s.pop();
}
if(s.empty())
return;
}
}
}
int main()
{
clock_t start, end;
float duration;
cout<<"Queen Place Problem:"<<endl;
cout<<"Input the value of n"<<endl;
int n;
cout<<" n>";
cin>>n;
int* path=new int[n];
for(int i=0;i<n;i++)
path[i]=-1000;
start=clock();
stack_stimu(n,path);
end=clock();
duration=end-start;
cout<<"the count is: "<<count<<endl;
cout<<"the duration is: "<<duration<<endl;
getchar();
getchar();
return 0;
}
monte carlo(蒙特卡罗)实现皇后寻找
//Random codes:
#include <iostream>
#include <ctime>
using namespace std;
#define N 19
int conflict(int *a)//求皇后之间冲突的冲突数
{
int b=0;
for(int i=1;i<N;i++)
{
for(int j=i+1;j<=N;j++)
{
if(abs(a[j]-a[i])==j-i) b++;
}
}
return b;
}
int main()
{
int result[N+1];
int temp;
srand((unsigned)time(NULL));
for(int i=0;i<=N;i++)//初始化result数组,由于初始化的特殊,可以避免同列情况
{
result[i]=i;
}
int con=conflict(result);//对当前result数组进行分析,确定冲突的数目
while(con!=0)
{
int rand1=0,rand2=0;
while(rand1==0||rand2==0)
{
rand1=rand()%(N+1);
rand2=rand()%(N+1);
}
temp=result[rand1];
result[rand1]=result[rand2];
result[rand2]=temp;
int con_temp=conflict(result);
if(con_temp>con)
{
temp=result[rand1];
result[rand1]=result[rand2];
result[rand2]=temp;
}
else
{
con=con_temp;
}
for(int i=1;i<N+1;i++)
{
cout<<result[i]<<" ";
}
cout<<endl;
}
cout<<"the result is:";
for(int k=1;k<=N;k++)
{
cout<<result[k]<<" ";
}
return 0;
}