problemscpp
A collection of my answers to algorithm problems in c++.
函数
acwing::acwing3472 命名空间参考

  1. 八皇后
更多...

函数

void dfs (vector< vector< bool > > board, int current_row, vector< string > &ans, vector< int > &ans_stk)
 
int main (istream &cin, ostream &cout)
 
 TEST (acwing3472, case1)
 

详细描述

  1. 八皇后

函数说明

◆ dfs()

void acwing::acwing3472::dfs ( vector< vector< bool > >  board,
int  current_row,
vector< string > &  ans,
vector< int > &  ans_stk 
)

在文件 acwing408.cpp1003 行定义.

1003 {
1004 if(current_row == 8) {
1005 ostringstream oss;
1006 for(int i = 0; i < 8; i++) {
1007 oss << ans_stk[i] + 1;
1008 }
1009 ans.push_back(oss.str());
1010 return;
1011 }
1012 for(int k = 0; k < 8; k++) {
1013 if(board[current_row][k]) {
1014 vector<vector<bool>> next_board = vector<vector<bool>>(board.begin(), board.end());
1015 for(int i = 0; i < 8; i++) {
1016 for(int j = 0; j < 8; j++) {
1017 if(i == current_row || j == k || i + j == current_row + k || i - j == current_row - k) {
1018 next_board[i][j] = false;
1019 }
1020 }
1021 }
1022 ans_stk.push_back(k);
1023 dfs(next_board, current_row + 1, ans, ans_stk);
1024 ans_stk.pop_back();
1025 }
1026 }
1027 }
void dfs(vector< vector< bool > > board, int current_row, vector< string > &ans, vector< int > &ans_stk)
Definition: acwing408.cpp:1003

引用了 dfs().

被这些函数引用 acwing::acwing3429::dfs(), dfs(), acwing::acwing3429::main() , 以及 main().

◆ main()

int acwing::acwing3472::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp1028 行定义.

1028 {
1029 int n, b;
1030 cin >> n;
1031 vector<vector<bool>> board = vector<vector<bool>>(8, vector<bool>(8, true));
1032 vector<string> ans = vector<string>();
1033 vector<int> ans_stk = vector<int>();
1034 dfs(board, 0, ans, ans_stk);
1035 while(n--) {
1036 cin >> b;
1037 cout << ans[b - 1] << endl;
1038 }
1039 return 0;
1040 }

引用了 dfs().

被这些函数引用 TEST().

◆ TEST()

acwing::acwing3472::TEST ( acwing3472  ,
case1   
)

在文件 acwing408_test.cpp1301 行定义.

1301 {
1302 istringstream in("2\n"
1303 "1\n"
1304 "92");
1305 auto out = ostringstream();
1306 main(in, out);
1307 const auto ans = out.str();
1308 ASSERT_EQ("15863724\n"
1309 "84136275\n",
1310 ans);
1311 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().