当前位置:网站首页>Leetcode51.n queen
Leetcode51.n queen
2022-07-04 03:10:00 【sakeww】
link :
https://leetcode-cn.com/problems/n-queens/
describe :

Example :


Code :
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
// Store all solutions by coordinate position
vector<vector<pair<int, int>>> solutions;
// The location of all queens in a solution
vector<pair<int, int>> solution;
DFS(solutions, solution, 0, n);
// Turn the coordinate position into string
return transResult(solutions, n);
}
void DFS(vector<vector<pair<int, int>>>& solutions, vector<pair<int, int>>& solution, int curRow, int n)
{
if (curRow == n) solutions.push_back(solution);
// Try whether a queen can be placed at each position of the current line
for (int col = 0; col < n; ++col) {
if (isValid(solution, curRow, col)) {
// If possible , Save the current location , Continue to determine the position of the queen in the next row
// Call the constructor directly , Internal construction pair, Or call make_pair
solution.emplace_back(curRow, col);
DFS(solutions, solution, curRow + 1, n);
// to flash back , Delete current location , Try another location on the current line
solution.pop_back();
}
}
}
// solution: One solution , From the first line to the previous line of the current line, each line has placed the Queen's point
bool isValid(vector<pair<int, int>>& solution, int row, int col) {
// Judge whether the queen position of the current line is in conflict with the queen position of the previous lines
// i.second == col: The first i Queens are in the same column as the current point
// i.first + i.second == row + col: The first i A queen is on the left with the current point , Abscissa + The ordinate values are the same
// i.first - i.second == row - col: The first i A queen is pressed with the current point , Abscissa - The ordinate values are the same
for (pair<int, int>& i : solution)
if (i.second == col || i.first + i.second == row + col
|| i.first - i.second == row - col)
return false;
return true;
}
vector<vector<string>> transResult(vector<vector<pair<int, int>>>& solutions, int n) {
vector<string> tmp();
// Turn every solution into string form , final result
vector<vector<string>> ret;
for (vector<pair<int, int>>& solution : solutions) {
//n*n char: Each row has n Elements , Change the Queen's position to Q
vector<string> solutionString(n, string(n, '.'));
for (pair<int, int>& i : solution) {
solutionString[i.first][i.second] = 'Q';
}
ret.push_back(solutionString);
}
return ret;
}
};
边栏推荐
- This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- 7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
- I stepped on a foundation pit today
- Résumé des outils communs et des points techniques de l'examen PMP
- The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
- Key knowledge of embedded driver
- Short math guide for latex by Michael downs
猜你喜欢

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan

Advanced learning of MySQL -- Application -- index

ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel

Node write API

C # learning notes: structure of CS documents

Redis transaction
![[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)](/img/36/e5b716f2f976eb474b673f85363dae.jpg)
[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)

(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)

What is cloud primordial?

Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
随机推荐
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
How to use websocket to realize simple chat function in C #
Gee import SHP data - crop image
Code Execution Vulnerability - no alphanumeric rce create_ function()
@Scheduled scheduled tasks
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
96% of the collected traffic is prevented by bubble mart of cloud hosting
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
CSCI 2134
Key knowledge of C language
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
[untitled]
Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
Remote work guide
Keep an IT training diary 055- moral bitch
Unity controls the selection of the previous and next characters
Imperial cms7.5 imitation "D9 download station" software application download website source code