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

分析

寻找N*N矩阵中放置N个皇后使其不会相互攻击的方案。使用回溯,一行一行的寻找可能的点,找到的话,继续找下一行,否则就退出。当找到一个可行的方案时候,需要将此时的二维数组的数据保存到最后的指针中。
C代码如下已通过:

int a[1000][1000];
//judge if(x,y)could be 1 in a[][]
bool place(int x,int y,int n)
{
    int i=0,j=0;
    for(i=0;i<x;i++)
        if(a[i][y]==1)  return false;
//search in left and top
    i=x-1;j=y-1;
    while(i>=0&&j>=0)
    {
        if(a[i][j]==1)return false;
        i--;j--;
    }
//search in right and top
    i=x-1;j=y+1;
    while(i>=0&&j<n)
    {
        if(a[i][j]==1)return false;
        i--;j++;
    }
//if ok return true
    return true;
}
void huisu(int no,int n,int *returnSize,char ***answer)
{
    if(no==n-1)
    {
        for(int i=0;i<n;i++)
        {
            if(place(n-1,i,n)==true)
            {
                a[n-1][i]=1;
                answer[*returnSize]=(char**)malloc(sizeof(char*)*n);
                for(int j=0;j<n;j++)
                {
                    answer[*returnSize][j]=(char*)malloc(sizeof(char)*(n+1));
                    for(int k=0;k<n;k++)
                    {
                        if(a[j][k]==0)
                            answer[*returnSize][j][k]='.';
                        else
                            answer[*returnSize][j][k]='Q';
                    }
                    answer[*returnSize][j][n]='\0';
                }
                *returnSize=*returnSize+1;
                a[n-1][i]=0;
            }
        }
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            if(place(no,i,n)==true)
            {
                a[no][i]=1;
                huisu(no+1,n,returnSize,answer);
                a[no][i]=0;
            }
        }
    }
}

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
char*** solveNQueens(int n, int* returnSize) {
    char ***ans=(char ***)malloc(sizeof(char **)*100000);
    *returnSize=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            a[i][j]=0;
    }
    huisu(0,n,returnSize,ans);
    return ans;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,768评论 0 33
  • 2016年10月16日满满一天的数学基础梳理虽然数学一直是弱项从小到大都是,可是听起来比英语有趣的多讲课老师好像高...
    肉酱兔阅读 216评论 0 1
  • 一直想经常写点文字来记录自己在每个时期的不同状态,不同目标,对各种各样事情的不同认知,不去管写的文字有多么拙劣...
    命硬heart阅读 1,216评论 0 2
  • FastReporter的交叉表会填充空数据是因为组织的数据缺少需要填充的位置所以没有填充,因此只要判断数据是否为...
    4d8b733229d9阅读 376评论 0 1
  • 生命的诞生是随着一团先天之炁的驻留,才使之鲜活起来,(不仅仅是因为有血有肉,因为刚刚死去的人也是有血有肉的),...
    我欲修仙阅读 296评论 0 1