leetcode 51 N皇后

我就是我 2022-09-14 09:27 212阅读 0赞

前言

题目:51. N 皇后

参考题解:N皇后-代码随想录


提交代码

题目要求:任何两个皇后都不能处于同一条横行、纵行或斜线上。

思路:按行遍历放置。用回溯解决。

我实现的代码,在处理能否放置的问题上,搞复杂了。虽然它是正确的。

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <cassert>
  5. #include <set>
  6. using namespace std;
  7. class Solution {
  8. private:
  9. vector<string> chessboard;
  10. vector<vector<string>> result;
  11. // 二维表示棋盘;棋盘中每个格子为一个集合,里面放入哪个Q占用
  12. vector<vector<set<pair<int,int>>>> used;
  13. public:
  14. bool Q_used(pair<int,int> loc, int cmd){
  15. // flag = 0, 放置一个Q,所占用其所在行,列,对角线
  16. // flag = 1, 拿走一个Q,所释放其所在行,列,对角线
  17. // flag = 2, 检查该位置,能否放置Q。true表示可以放置,false表示不可放置
  18. const int n = used.size();
  19. const int row = loc.first;
  20. const int col = loc.second;
  21. if(cmd == 0){
  22. for(int i=0; i<n; i++) // 所在行累计标记flag
  23. used[row][i].insert({row,col});
  24. for(int i=0; i<n; i++) // 所在列累计标记flag
  25. used[i][col].insert({row,col});
  26. for(int i=row-1, j=col-1; i>=0 && j>=0; i--, j--) // 左上方对角线,累计标记flag
  27. used[i][j].insert({row,col});
  28. for(int i=row+1, j=col+1; i<n && j<n; i++, j++) // 右下方对角线,累计标记flag
  29. used[i][j].insert({row,col});
  30. for(int i=row-1, j=col+1; i>=0 && j<n; i--, j++) // 右上方对角线,累计标记flag
  31. used[i][j].insert({row,col});
  32. for(int i=row+1, j=col-1; i<n && j>=0; i++, j--) // 右上方对角线,累计标记flag
  33. used[i][j].insert({row,col});
  34. return true;
  35. }else if(cmd == 1){
  36. for(int i=0; i<n; i++)
  37. used[row][i].erase({row,col});
  38. for(int i=0; i<n; i++)
  39. used[i][col].erase({row,col});
  40. for(int i=row-1, j=col-1; i>=0 && j>=0; i--, j--)
  41. used[i][j].erase({row,col});
  42. for(int i=row+1, j=col+1; i<n && j<n; i++, j++)
  43. used[i][j].erase({row,col});
  44. for(int i=row-1, j=col+1; i>=0 && j<n; i--, j++)
  45. used[i][j].erase({row,col});
  46. for(int i=row+1, j=col-1; i<n && j>=0; i++, j--)
  47. used[i][j].erase({row,col});
  48. return true;
  49. }else if(cmd == 2){
  50. if(used[row][col].empty())
  51. return true;
  52. else
  53. return false;
  54. }else{
  55. cout<<"no such cmd"<<endl;
  56. exit(0);
  57. }
  58. }
  59. void backTracking(int start_row){
  60. // 出口之一: 当n*n的棋盘,成功放入n个Q
  61. if(start_row == used.size()){
  62. result.push_back(chessboard);
  63. return;
  64. }
  65. for(int i=0; i<used.size(); i++){
  66. if(Q_used({start_row,i},2) == false) // 检查{start_row,i}位置能否放入Q
  67. continue;
  68. Q_used({start_row,i},0); // 在{start_row,i}位置放入Q
  69. chessboard[start_row][i] = 'Q';
  70. backTracking(start_row+1); // 在下一行找合适的位置放置Q
  71. chessboard[start_row][i] = '.';
  72. Q_used({start_row,i},1); // 在{start_row,i}位置拿走Q
  73. }
  74. }
  75. vector<vector<string>> solveNQueens(int n) {
  76. chessboard.resize(n,string(n,'.'));
  77. used.resize(n,vector<set<pair<int,int>>>(n,set<pair<int,int>>{}));
  78. backTracking(0);
  79. return result;
  80. }
  81. };
  82. int main(void){
  83. int n = 4;
  84. Solution s;
  85. vector<vector<string>> result = s.solveNQueens(6);
  86. for(auto one : result){
  87. for(auto str : one)
  88. cout<<str<<" ";
  89. cout<<endl;
  90. }
  91. }

参考题解,在这方面做的更好。放置之前进行动态判断,不需要辅助空间。

下面代码来自参考题解。

  1. class Solution {
  2. private:
  3. vector<vector<string>> result;
  4. // n 为输入的棋盘大小
  5. // row 是当前递归到棋牌的第几行了
  6. void backtracking(int n, int row, vector<string>& chessboard) {
  7. if (row == n) {
  8. result.push_back(chessboard);
  9. return;
  10. }
  11. for (int col = 0; col < n; col++) {
  12. if (isValid(row, col, chessboard, n)) { // 验证合法就可以放
  13. chessboard[row][col] = 'Q'; // 放置皇后
  14. backtracking(n, row + 1, chessboard);
  15. chessboard[row][col] = '.'; // 回溯,撤销皇后
  16. }
  17. }
  18. }
  19. bool isValid(int row, int col, vector<string>& chessboard, int n) {
  20. int count = 0;
  21. // 检查列
  22. for (int i = 0; i < row; i++) { // 这是一个剪枝
  23. if (chessboard[i][col] == 'Q') {
  24. return false;
  25. }
  26. }
  27. // 检查 45度角是否有皇后
  28. for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
  29. if (chessboard[i][j] == 'Q') {
  30. return false;
  31. }
  32. }
  33. // 检查 135度角是否有皇后
  34. for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
  35. if (chessboard[i][j] == 'Q') {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. public:
  42. vector<vector<string>> solveNQueens(int n) {
  43. result.clear();
  44. std::vector<std::string> chessboard(n, std::string(n, '.'));
  45. backtracking(n, 0, chessboard);
  46. return result;
  47. }
  48. };

发表评论

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

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

相关阅读

    相关 leetcode 51N皇后 52N皇后II

    n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 给定一个整数 n,返回所有不同的 n 皇

    相关 LeetCode 51. N皇后

    题目重述 n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方

    相关 leetcode:51. N皇后

    题目: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 给定一个整数 n,返回所有

    相关 Leetcode51N皇后

    n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 思路: 刚开始的思路是,用一个二维int数组来保存,后来发现很繁琐,其次就