LeetCode开心刷题二十七天——51. N-Queens

小灰灰 2023-08-17 15:14 137阅读 0赞

Bonus:

const修饰的i我们称之为符号常量。即,i不能在其他地方被重新赋值了。注意:const int i与int const i是等价的

  1. N-Queens

Hard

104047FavoriteShare

The n-queens puzzle is the problem of placing n queens on an n×nchessboard such that no two queens attack each other.

8-queens.png

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example:

  1. Input: 4
  2. Output: [
  3. [".Q..", // Solution 1
  4. "...Q",
  5. "Q...",
  6. "..Q."],
  7. ["..Q.", // Solution 2
  8. "Q...",
  9. "...Q",
  10. ".Q.."]
  11. ]
  12. Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
  13. This is a classic problem that has been bothering me for a long time,use back-tracing,finally I
  14. can't finish it without looking the answer.SO this problem deverse repeated analysis and trial
  15. The huahua's answer exchange the position of x and y,but I recovered in my answer!
  16. a
  17. #include<stdio.h>
  18. #include<iostream>
  19. #include<vector>
  20. #include<algorithm>
  21. #include<stack>
  22. #include<string>
  23. #include<memory>
  24. #include<memory.h>
  25. #include<hash_map>
  26. #include<map>
  27. #include<list>
  28. #include<set>
  29. #include<math.h>
  30. #include<unordered_map>
  31. using namespace std;
  32. class Solution {
  33. public:
  34. vector<vector<string>> solveNQueens(int n) {
  35. ans_.clear();
  36. tmp_=vector<string>(n,string(n,'.'));
  37. cols_=vector<int>(n,0);
  38. //row_=vector<int>(n,0);
  39. diag1_=vector<int>(2*n-1,0);
  40. diag2_=vector<int>(2*n-1,0);
  41. nqueens(n,0);
  42. for(auto i:ans_)
  43. {
  44. for(auto j:i)
  45. {
  46. cout<<j<<endl;
  47. }
  48. cout<<endl;
  49. cout<<endl;
  50. }
  51. return ans_;
  52. }
  53. private:
  54. vector<vector<string>> ans_;
  55. vector<string> tmp_;
  56. vector<int> cols_;
  57. //don't need row,we loop by row.it don't have chance duplicate
  58. //vector<int> row_;
  59. vector<int> diag1_;
  60. vector<int> diag2_;
  61. void nqueens(const int n,const int x)
  62. {
  63. if(x==n)
  64. {
  65. ans_.push_back(tmp_);
  66. return;
  67. }
  68. for(int y=0;y<n;y++)
  69. {
  70. if(!available(x,y,n)) continue;
  71. put_(x,y,n,1);
  72. nqueens(n,x+1);
  73. put_(x,y,n,0);
  74. }
  75. }
  76. bool available(int x,int y,int n)
  77. {
  78. return !cols_[y]
  79. &&!diag1_[x+y]
  80. &&!diag2_[x-y+n-1];
  81. }
  82. void put_(int x,int y,int n,bool is_put)
  83. {
  84. cols_[y]=is_put;
  85. diag1_[x+y]=is_put;
  86. diag2_[x-y+n-1]=is_put;
  87. tmp_[x][y]=is_put?'Q':'.';
  88. }
  89. };
  90. int main()
  91. {
  92. Solution s;
  93. int n;
  94. cin>>n;
  95. s.solveNQueens(n);
  96. }

Bonus:

https://zhuanlan.zhihu.com/p/33090662

https://blog.csdn.net/mo_yihua/article/details/51627809

Two function to handle the input:

  input() 除此外还可以接受表达式

  raw_input() 读取一行,返回字符串

special Action:

  Because these two functions can only handle string,so use these two methods to change it.

    1.Forced type conservation

    2.eval() function

      

  1. a = input("请输入:")
  2. 请输入:123
  3. >>> type(a)
  4. <class 'str'>
  5. a = int(input("请输入:"))
  6. 请输入:123
  7. >>> type(a)
  8. <class 'int'>
  9. 相当于整数赋值,eval帮我们去除了引号
  10. a = eval(input("请输入:"))
  11. 请输入:123
  12. >>> type(a)
  13. <class 'int'>
  14. input([prompt]) 函数和 raw_input([prompt]) 函数基本类似,但是 input 可以接收一个Python表达式作为输入,并将运算结果返回。
  15. #!/usr/bin/python
  16. # -*- coding: UTF-8 -*-
  17. str = input("请输入:");
  18. print "你输入的内容是: ", str
  19. 这会产生如下的对应着输入的结果:
  20. 请输入:[x*5 for x in range(2,10,2)]
  21. 你输入的内容是: [10, 20, 30, 40]

转载于:https://www.cnblogs.com/Marigolci/p/11274267.html

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读