日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

52. N-Queens II

發(fā)布時(shí)間:2024/8/26 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 52. N-Queens II 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

description:

八皇后
Note:

Example:

Input: 4 Output: 2 Explanation: There are two distinct solutions to the 4-queens puzzle as shown below. [[".Q..", // Solution 1"...Q","Q...","..Q."],["..Q.", // Solution 2"Q...","...Q",".Q.."] ]

answer:

class Solution { public:int totalNQueens(int n) {int res = 0;vector<int> pos(n, -1);helper(pos, 0, res);return res;}void helper(vector<int>& pos, int row, int& res) {int n = pos.size();if (row == n) ++res;for (int col = 0; col < n; ++col) {if (isValid(pos, row, col)) {pos[row] = col;helper(pos, row + 1, res);pos[row] = -1;}}}bool isValid(vector<int>& pos, int row, int col) {for (int i = 0; i < row; ++i) {if (col == pos[i] || abs(row - i) == abs(col - pos[i])) {return false;}}return true;} };

relative point get√:

hint :

和上面那個(gè)題一樣

轉(zhuǎn)載于:https://www.cnblogs.com/forPrometheus-jun/p/11246876.html

總結(jié)

以上是生活随笔為你收集整理的52. N-Queens II的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。