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個(gè)皇后使其不會(huì)相互攻擊的方案。使用回溯,一行一行的尋找可能的點(diǎn),找到的話,繼續(xù)找下一行,否則就退出。當(dāng)找到一個(gè)可行的方案時(shí)候,需要將此時(shí)的二維數(shù)組的數(shù)據(jù)保存到最后的指針中。
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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,891評論 0 33
  • 2016年10月16日滿滿一天的數(shù)學(xué)基礎(chǔ)梳理雖然數(shù)學(xué)一直是弱項(xiàng)從小到大都是,可是聽起來比英語有趣的多講課老師好像高...
    肉醬兔閱讀 238評論 0 1
  • 一直想經(jīng)常寫點(diǎn)文字來記錄自己在每個(gè)時(shí)期的不同狀態(tài),不同目標(biāo),對各種各樣事情的不同認(rèn)知,不去管寫的文字有多么拙劣...
    命硬heart閱讀 1,280評論 0 2
  • FastReporter的交叉表會(huì)填充空數(shù)據(jù)是因?yàn)榻M織的數(shù)據(jù)缺少需要填充的位置所以沒有填充,因此只要判斷數(shù)據(jù)是否為...
    4d8b733229d9閱讀 425評論 0 1
  • 生命的誕生是隨著一團(tuán)先天之炁的駐留,才使之鮮活起來,(不僅僅是因?yàn)橛醒腥?,因?yàn)閯倓偹廊サ娜艘彩怯醒腥獾模?..
    我欲修仙閱讀 313評論 0 1

友情鏈接更多精彩內(nèi)容