当前位置:网站首页>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;
}
};
边栏推荐
- 2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
- No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- 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
- 中電資訊-信貸業務數字化轉型如何從星空到指尖?
- [UE4] parse JSON string
- Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
- System integration meets the three business needs of enterprises
- The "two-way link" of pushing messages helps app quickly realize two-way communication capability
- PMP 考試常見工具與技術點總結
猜你喜欢

C language black Technology: Archimedes spiral! Novel, interesting, advanced~

Advanced learning of MySQL -- Application -- index

Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi

How to use websocket to realize simple chat function in C #

Webhook triggers Jenkins for sonar detection

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

Node write API

Add token validation in swagger

Advanced learning of MySQL -- Application -- storage engine

Jenkins continuous integration environment construction V (Jenkins common construction triggers)
随机推荐
基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
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
The requests module uses
How to pipe several commands in Go?
POSTECH | option compatible reward reverse reinforcement learning
Remote work guide
Rhcsa day 2
How to use STR function of C language
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
Gee import SHP data - crop image
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
Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
MySQL data query optimization -- data structure of index
Setting methods, usage methods and common usage scenarios of environment variables in postman
Sword finger offer:55 - I. depth of binary tree
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
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso