当前位置:网站首页>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;
}
};
边栏推荐
- Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
- Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
- Practical multifunctional toolbox wechat applet source code / support traffic master
- Database concept and installation
- Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
- The difference between MCU serial communication and parallel communication and the understanding of UART
- Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
- Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
- Bugku Zhi, you have to stop him
- Global and Chinese market of cell scrapers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
@Scheduled scheduled tasks
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Node solves cross domain problems
MySQL workbench use
Imperial cms7.5 imitation "D9 download station" software application download website source code
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Teach you how to optimize SQL
Setting methods, usage methods and common usage scenarios of environment variables in postman
随机推荐
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
[latex] production of complex tables: excel2latex and detail adjustment
false sharing
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
中電資訊-信貸業務數字化轉型如何從星空到指尖?
Li Chuang EDA learning notes IX: layers
STM32 key content
Setting methods, usage methods and common usage scenarios of environment variables in postman
WordPress collection WordPress hang up collection plug-in
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
Ai aide à la recherche de plagiat dans le design artistique! L'équipe du professeur Liu Fang a été embauchée par ACM mm, une conférence multimédia de haut niveau.
Experience summary of the 12th Blue Bridge Cup (written for the first time)
Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
[development team follows] API specification
what does ctrl + d do?
Lichuang EDA learning notes 14: PCB board canvas settings
Recent learning fragmentation (14)
Backpropagation formula derivation [Li Hongyi deep learning version]