problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 静态 Private 成员函数 | 所有成员列表
leetcode::n_queens::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< vector< string > > solveNQueens (int n)
 

静态 Private 成员函数

static void dfs (const vector< vector< bool > > &board, int line, int n, vector< vector< string > > &ans)
 
static vector< string > toStringVec (const vector< vector< bool > > &board)
 
static bool valid (const vector< vector< bool > > &board, int n, int x, int y)
 

详细描述

在文件 leetcode.h2974 行定义.

成员函数说明

◆ dfs()

void leetcode::n_queens::Solution::dfs ( const vector< vector< bool > > &  board,
int  line,
int  n,
vector< vector< string > > &  ans 
)
staticprivate

在文件 leetcode.cpp8315 行定义.

8315 {
8316 if(line == n) {
8317 ans.emplace_back(toStringVec(board));
8318 return;
8319 }
8320 for(int i = 0; i < n; i++) {
8321 if(valid(board, n, line, i)) {
8322 vector<vector<bool>> next = board;
8323 next[line][i] = true;
8324 dfs(next, line + 1, n, ans);
8325 }
8326 }
8327 }
static bool valid(const vector< vector< bool > > &board, int n, int x, int y)
Definition: leetcode.cpp:8329
static vector< string > toStringVec(const vector< vector< bool > > &board)
Definition: leetcode.cpp:8348
static void dfs(const vector< vector< bool > > &board, int line, int n, vector< vector< string > > &ans)
Definition: leetcode.cpp:8315

引用了 dfs(), toStringVec() , 以及 valid().

被这些函数引用 dfs() , 以及 solveNQueens().

◆ solveNQueens()

vector< vector< string > > leetcode::n_queens::Solution::solveNQueens ( int  n)
static

在文件 leetcode.cpp8308 行定义.

8308 {
8309 vector<vector<string>> ans;
8310 const vector board(n, vector(n, false));
8311 dfs(board, 0, n, ans);
8312 return ans;
8313 }

引用了 dfs().

被这些函数引用 leetcode::n_queens::TEST().

◆ toStringVec()

vector< string > leetcode::n_queens::Solution::toStringVec ( const vector< vector< bool > > &  board)
staticprivate

在文件 leetcode.cpp8348 行定义.

8348 {
8349 vector<string> ans(board.size());
8350 for(int i = 0; i < board.size(); i++) {
8351 ostringstream oss;
8352 for(int j = 0; j < board[i].size(); j++) {
8353 oss << (board[i][j] ? 'Q' : '.');
8354 }
8355 ans[i] = oss.str();
8356 }
8357 return ans;
8358 }

被这些函数引用 dfs().

◆ valid()

bool leetcode::n_queens::Solution::valid ( const vector< vector< bool > > &  board,
int  n,
int  x,
int  y 
)
staticprivate

在文件 leetcode.cpp8329 行定义.

8329 {
8330 for(int i = 0; i < x; i++) {
8331 if(board[i][y]) {
8332 return false;
8333 }
8334 }
8335 for(int i = x, j = y; i >= 0 && i < n && j >= 0 && j < n; i--, j--) {
8336 if(board[i][j]) {
8337 return false;
8338 }
8339 }
8340 for(int i = x, j = y; i >= 0 && i < n && j >= 0 && j < n; i--, j++) {
8341 if(board[i][j]) {
8342 return false;
8343 }
8344 }
8345 return true;
8346 }

被这些函数引用 dfs().


该类的文档由以下文件生成: