leetcode八皇后


private:	  
void put_down_the_queen(int x,int y,vector<vector<int>>&mark)
{
    static const int dx[]={-1,1,0,0,-1,-1,1,1};
	static const int dy[]={0,0,-1,1,-1,1,-1,1};
	mark[x][y]=1;
	for(int i=1;i<mark.size();i++)
	  for(int j =0;j<8;j++)
	  {
	      int new_x=x+dx[j];
		  int new_y=y=dy[j];
		  if(new_x>=0&&new_x<=mark.size()&&new_y>=0&&new_y<=mark.size())
		  {
		      mark[new_x][new_y]=1;
		  }
	  }
	   
	}
}  
void genarate(int k,int n,vector<string> &location,vector<vector<string>> &result,vector<,vector<int>> &mark)
{
    if(k==n)
	{
	    result.push_back(location);
	    return;
	}
	for(int i=0;i<n;i++)
	{
	    if(mark[k][i]==0)
		{
		   vector<vector<int>> temp=mark;
		   location[k][i]='Q';
		   put_down_the_queen(k,i,mark);
		   genarate(k+1,i,result,mark);
		   mark=temp;
		   location[k][i]='.';
		}
	}
	
}          	  
int main()
{
    vector<vector<string>> result;
	Solution solve;
	result=solve.solveQueens(4);
	for(int i=0;i<result.size();i++)
	{
	    printf(" i=%d\n",i);
		for(int j=0;j<result.size();j++)
		{
		   printf("%s\n",result[i][j].c_str());
		   
		)
		printf("\n");
	}
	return 0;
}
	 
发布了8 篇原创文章 · 获赞 2 · 访问量 724

猜你喜欢

转载自blog.csdn.net/weixin_41375103/article/details/104147661