当前位置:网站首页>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;
}
};
边栏推荐
- Pagoda SSL can't be accessed? 443 port occupied? resolvent
- Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
- There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
- A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
- 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
- Node write API
- Sword finger offer:55 - I. depth of binary tree
- Advanced learning of MySQL -- Application -- index
- Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
- 3D game modeling is in full swing. Are you still confused about the future?
猜你喜欢
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Crawler practice website image batch download
Have you entered the workplace since the first 00???
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
Dare to climb here, you're not far from prison, reptile reverse actual combat case
Webhook triggers Jenkins for sonar detection
Pagoda SSL can't be accessed? 443 port occupied? resolvent
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
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
随机推荐
Keep an IT training diary 054- opening and closing
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
Rhcsa day 2
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
C learning notes: C foundation - Language & characteristics interpretation
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
Pagoda SSL can't be accessed? 443 port occupied? resolvent
CSCI 2134
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
Talking about custom conditions and handling errors in MySQL Foundation
Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
VRRP+BFD
Code Execution Vulnerability - no alphanumeric rce create_ function()
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
Imperial cms7.5 imitation "D9 download station" software application download website source code