fork download
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. vector<vector<string> > ans;
  11. bool safe(vector<string>& board, int i, int j){
  12. if (board[i][j]=='Q')return false;
  13. int n = board.size();
  14. for(int a=0; a<i; ++a)
  15. if ( board[a][j]=='Q')return false;
  16.  
  17. for(int k1=i-1, k2=j-1; k1>=0 and k2>=0; k1--, k2--)
  18. if (board[k1][k2]=='Q')return false;
  19.  
  20. for(int k1=i-1, k2=j+1; k1>=0 and k2<n; k1--, k2++)
  21. if (board[k1][k2]=='Q')return false;
  22.  
  23. return true;
  24. }
  25. bool solveNQueens(int n) {
  26. vector<string> board(n, string(n, '.'));
  27.  
  28. int breakBound = 0;
  29. if ( (2*n)%3==0)breakBound = (2*n/3 + 1);
  30. else breakBound = ceil(2.0*n/3.0);
  31.  
  32. return solve(board, breakBound,-1);
  33. }
  34. bool solve(vector<string>& board, int queens_left, int last_i){
  35. if (queens_left==0){
  36. cout << "FALSE CONJECTURE";
  37. ans.push_back(board);
  38. return true;
  39. }
  40. int n = board.size();
  41. for(int j=(last_i+1)%2; j<n; j+=2){
  42. if (safe(board, last_i+1, j)){
  43. board[last_i+1][j] = 'Q';
  44. bool s = solve(board, queens_left-1, last_i+1);
  45. if (s)return true;
  46. board[last_i+1][j] = '.';
  47. }
  48. }
  49. return false;
  50. }
  51. };
  52.  
  53. int main(int argc, char** argv) {
  54. Solution S;
  55. cout << S.solveNQueens(25) << endl;
  56. for(auto& board : S.ans){
  57. for(int i=0; i<board.size(); ++i){
  58. for(int j=0; j<board[i].size(); ++j){
  59. cout << board[i][j] << " ";
  60. }
  61. cout << endl;
  62. }
  63. }
  64. cout << endl << endl;
  65. }
  66.  
Success #stdin #stdout 1.01s 5532KB
stdin
Standard input is empty
stdout
FALSE CONJECTURE1
. . . . Q . . . . . . . . . . . . . . . . . . . . 
. . . . . . . Q . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . Q . . . . 
. . . . . . . . . . . . . . . . . Q . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . Q 
. . . . . . . . . . . . . . . . . . . . . Q . . . 
Q . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . Q . . . . . . . . . . . . . . . . . . . 
. . . . . . . . Q . . . . . . . . . . . . . . . . 
. Q . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . Q . . 
. . . . . . . . . . . . . . . . . . . Q . . . . . 
. . Q . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . Q . 
. . . . . . . . . . Q . . . . . . . . . . . . . . 
. . . Q . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . Q . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . .