当前位置:网站首页>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;
}
};
边栏推荐
- Sword finger offer:55 - I. depth of binary tree
- Leetcode 110 balanced binary tree
- How to use STR function of C language
- Dare to climb here, you're not far from prison, reptile reverse actual combat case
- Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Career development direction
- Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
- 2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
- Jenkins continuous integration environment construction V (Jenkins common construction triggers)
- The "two-way link" of pushing messages helps app quickly realize two-way communication capability
猜你喜欢

Network byte order

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"

AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用

3D game modeling is in full swing. Are you still confused about the future?

Unity knapsack system (code to center and exchange items)

Redis transaction

No clue about the data analysis report? After reading this introduction of smartbi, you will understand!

Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
![Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]](/img/0d/7a8370d153a8479b706377c3487220.jpg)
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
随机推荐
Keep an IT training diary 055- moral bitch
Recent learning fragmentation (14)
Add token validation in swagger
[database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
Rhcsa day 2
What are the conditions for the opening of Tiktok live broadcast preview?
Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
Node solves cross domain problems
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
Talking about custom conditions and handling errors in MySQL Foundation
static hostname; transient hostname; pretty hostname
Is online futures account opening safe and reliable? Which domestic futures company is better?
What is cloud primordial?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
How to use STR function of C language
MySQL query
Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
What is the difference between enterprise wechat applet and wechat applet