C++實現(xiàn)LeetCode(51.N皇后問題)
[LeetCode] 51. N-Queens N皇后問題
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.
Example:
Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
經(jīng)典的N皇后問題,基本所有的算法書中都會包含的問題。可能有些人對國際象棋不太熟悉,大家都知道中國象棋中最叼的是車,橫豎都能走,但是在國際象棋中還有更叼的,就是皇后,不但能橫豎走,還能走兩個斜線,有如 bug 一般的存在。所以經(jīng)典的八皇后問題就應運而生了,在一個 8x8 大小的棋盤上如果才能放8個皇后,使得兩兩之間不能相遇,所謂一山不能容二虎,而這里有八個母老虎,互相都不能相遇。對于這類問題,沒有太簡便的方法,只能使用窮舉法,就是嘗試所有的組合,每放置一個新的皇后的時候,必須要保證跟之前的所有皇后不能沖突,若發(fā)生了沖突,說明當前位置不能放,要重新找地方,這個邏輯非常適合用遞歸來做。我們先建立一個長度為 nxn 的全是點的數(shù)組 queens,然后從第0行開始調用遞歸。在遞歸函數(shù)中,我們首先判斷當前行數(shù)是否已經(jīng)為n,是的話,說明所有的皇后都已經(jīng)成功放置好了,所以我們只要將 queens 數(shù)組加入結果 res 中即可。否則的話,我們遍歷該行的所有列的位置,行跟列的位置都確定后,我們要驗證當前位置是否會產(chǎn)生沖突,那么就需要使用一個子函數(shù)來判斷了,首先驗證該列是否有沖突,就遍歷之前的所有行,若某一行相同列也有皇后,則沖突返回false;再驗證兩個對角線是否沖突,就是一些坐標轉換,主要不要寫錯了,若都沒有沖突,則說明該位置可以放皇后,放了新皇后之后,再對下一行調用遞歸即可,注意遞歸結束之后要返回狀態(tài),參見代碼如下:
解法一:
class Solution { public: vector<vector<string>> solveNQueens(int n) { vector<vector<string>> res; vector<string> queens(n, string(n, '.')); helper(0, queens, res); return res; } void helper(int curRow, vector<string>& queens, vector<vector<string>>& res) { int n = queens.size(); if (curRow == n) { res.push_back(queens); return; } for (int i = 0; i < n; ++i) { if (isValid(queens, curRow, i)) { queens[curRow][i] = 'Q'; helper(curRow + 1, queens, res); queens[curRow][i] = '.'; } } } bool isValid(vector<string>& queens, int row, int col) { for (int i = 0; i < row; ++i) { if (queens[i][col] == 'Q') return false; } for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j) { if (queens[i][j] == 'Q') return false; } for (int i = row - 1, j = col + 1; i >= 0 && j < queens.size(); --i, ++j) { if (queens[i][j] == 'Q') return false; } return true; } };
我們還可以只使用一個一維數(shù)組 queenCol 來保存所有皇后的列位置,初始化均為-1, 那么 queenCol[i] 就是表示第i個皇后在 (i, queenCol[i]) 位置,遞歸函數(shù)還是跟上面的解法相同,就是在當前行數(shù)等于n的時候,我們要將 queenCol 還原成一個 nxn 大小的矩陣,并存入結果 res 中。這種記錄每個皇后的坐標的方法在驗證沖突的時候比較簡單,只要從第0行遍歷到當前行,若跟之前的皇后的列數(shù)相同,直接返回false,叼就叼在判斷對角線沖突非常簡便,因為當兩個點在同一條對角線上,那么二者的橫坐標差的絕對值等于縱坐標差的絕對值,利用這條性質,可以快速的判斷沖突,代碼如下:
解法二:
class Solution { public: vector<vector<string>> solveNQueens(int n) { vector<vector<string>> res; vector<int> queenCol(n, -1); helper(0, queenCol, res); return res; } void helper(int curRow, vector<int>& queenCol, vector<vector<string>>& res) { int n = queenCol.size(); if (curRow == n) { vector<string> out(n, string(n, '.')); for (int i = 0; i < n; ++i) { out[i][queenCol[i]] = 'Q'; } res.push_back(out); return; } for (int i = 0; i < n; ++i) { if (isValid(queenCol, curRow, i)) { queenCol[curRow] = i; helper(curRow + 1, queenCol, res); queenCol[curRow] = -1; } } } bool isValid(vector<int>& queenCol, int row, int col) { for (int i = 0; i < row; ++i) { if (col == queenCol[i] || abs(row - i) == abs(col - queenCol[i])) return false; } return true; } };
到此這篇關于C++實現(xiàn)LeetCode(51.N皇后問題)的文章就介紹到這了,更多相關C++實現(xiàn)N皇后問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- C++實現(xiàn)LeetCode(94.二叉樹的中序遍歷)
- C++實現(xiàn)LeetCode(112.二叉樹的路徑和)
- C++實現(xiàn)LeetCode(78.子集合)
- C++實現(xiàn)LeetCode(47.全排列之二)
- C++實現(xiàn)LeetCode(90.子集合之二)
- C++實現(xiàn)LeetCode(113.二叉樹路徑之和之二)
- C++實現(xiàn)LeetCode(39.組合之和)
- C++實現(xiàn)LeetCode(38.計數(shù)和讀法)
- C++實現(xiàn)LeetCode(77.Combinations 組合項)
- C++實現(xiàn)LeetCode(46.全排列)
- C++實現(xiàn)LeetCode(43.字符串相乘)
相關文章
C++ OpenCV單峰三角閾值法Thresh_Unimodal詳解
本文主要介紹了適合當圖像的直方圖具有明顯單峰特征時使用,結合了三角法的原理而設計的圖像分割方法,感興趣的小伙伴可以了解一下2021-12-12聊聊Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題
這篇文章主要介紹了Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題,給大家介紹了圖像的克隆及拷貝問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01基于C語言實現(xiàn)創(chuàng)意多彩貪吃蛇游戲
這篇文章主要介紹了如何利用C語言實現(xiàn)一個創(chuàng)意多彩貪吃蛇游戲,這是一個純C語言外加easyx庫的繪圖函數(shù)制作而成的有趣小游戲,無需引入額外資源,感興趣的可以動手嘗試一下2022-08-08