The first code:
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 BOOLIslegal (intIintJ, vector<string> &Current )7 {8 intSize =current.size ();9 intx = i1, y =J;Ten while(x >=0) One { A if(Current[x][y] = ='Q') - return false; -x--; the } -x = i1; -y = J-1; - while(x >=0&& y >=0) + { - if(Current[x][y] = ='Q') + return false; Ax--; aty--; - } -x = i-1; -y = j +1; - while(x >=0&& y <size) - { in if(Current[x][y] = ='Q') - return false; tox--; +y++; - } the return true; * } $ Panax Notoginseng voidGetResult (intRowintIndex, vector<vector<string>> &result, vector<string> ¤t,intsize) - { the if(Row = =size) + Result.push_back (current); A Else the { + while(Index <size) - { $Current[row][index] ='Q'; $ if(Islegal (row, index, current)) -GetResult (Row +1,0, result, current, size); -Current[row][index] ='.'; theindex++; - }Wuyi } the } - Wuvector<vector<string>> Solvenqueens (intN) - { Aboutvector<vector<string>>result; $ strings =""; - for(inti =0; I < n; i++) -S.push_back ('.'); -vector<string>Current (n, s); AGetResult (0,0, result, current, n); + returnresult; the } - $ intMain () the { thevector<vector<string>> result = Solvenqueens (4); the for(inti =0; I < result.size (); i++) the { - for(intj =0; J < Result[i].size (); J + +) incout << result[i][j].c_str () <<Endl; thecout <<"-_________________________________________-"<<Endl; the } About return 0; the}
The second code:
Have, forget to save, forget, do not post, this problem also to do this, discussion area of that use bit of I really admire dead you ...!!!!!!!!!
Leetcode n-queens I && n-queens II