返回介绍

lcci / 08.12.Eight Queens / README_EN

发布于 2024-06-17 01:04:43 字数 6314 浏览 0 评论 0 收藏 0

08.12. Eight Queens

中文文档

Description

Write an algorithm to print all ways of arranging n queens on an n x n chess board so that none of them share the same row, column, or diagonal. In this case, "diagonal" means all diagonals, not just the two that bisect the board.

Notes: This problem is a generalization of the original one in the book.

Example:


 Input: 4

 Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]

 Explanation: 4 queens has following two solutions

[

 [".Q..",  // solution 1

  "...Q",

  "Q...",

  "..Q."],



 ["..Q.",  // solution 2

  "Q...",

  "...Q",

  ".Q.."]

]

Solutions

Solution 1

class Solution:
  def solveNQueens(self, n: int) -> List[List[str]]:
    res = []
    g = [['.'] * n for _ in range(n)]
    col = [False] * n
    dg = [False] * (2 * n)
    udg = [False] * (2 * n)

    def dfs(u):
      if u == n:
        res.append([''.join(item) for item in g])
        return
      for i in range(n):
        if not col[i] and not dg[u + i] and not udg[n - u + i]:
          g[u][i] = 'Q'
          col[i] = dg[u + i] = udg[n - u + i] = True
          dfs(u + 1)
          g[u][i] = '.'
          col[i] = dg[u + i] = udg[n - u + i] = False

    dfs(0)
    return res
class Solution {
  public List<List<String>> solveNQueens(int n) {
    List<List<String>> res = new ArrayList<>();
    String[][] g = new String[n][n];
    for (int i = 0; i < n; ++i) {
      String[] t = new String[n];
      Arrays.fill(t, ".");
      g[i] = t;
    }
    // 列是否已经有值
    boolean[] col = new boolean[n];
    // 斜线是否已经有值
    boolean[] dg = new boolean[2 * n];
    // 反斜线是否已经有值
    boolean[] udg = new boolean[2 * n];
    // 从第一行开始搜索
    dfs(0, n, col, dg, udg, g, res);
    return res;
  }

  private void dfs(int u, int n, boolean[] col, boolean[] dg, boolean[] udg, String[][] g,
    List<List<String>> res) {
    if (u == n) {
      List<String> t = new ArrayList<>();
      for (String[] e : g) {
        t.add(String.join("", e));
      }
      res.add(t);
      return;
    }
    for (int i = 0; i < n; ++i) {
      if (!col[i] && !dg[u + i] && !udg[n - u + i]) {
        g[u][i] = "Q";
        col[i] = dg[u + i] = udg[n - u + i] = true;
        dfs(u + 1, n, col, dg, udg, g, res);
        g[u][i] = ".";
        col[i] = dg[u + i] = udg[n - u + i] = false;
      }
    }
  }
}
class Solution {
public:
  vector<vector<string>> solveNQueens(int n) {
    vector<vector<string>> res;
    vector<string> g(n, string(n, '.'));
    vector<bool> col(n, false);
    vector<bool> dg(2 * n, false);
    vector<bool> udg(2 * n, false);
    dfs(0, n, col, dg, udg, g, res);
    return res;
  }

  void dfs(int u, int n, vector<bool>& col, vector<bool>& dg, vector<bool>& udg, vector<string>& g, vector<vector<string>>& res) {
    if (u == n) {
      res.push_back(g);
      return;
    }
    for (int i = 0; i < n; ++i) {
      if (!col[i] && !dg[u + i] && !udg[n - u + i]) {
        g[u][i] = 'Q';
        col[i] = dg[u + i] = udg[n - u + i] = true;
        dfs(u + 1, n, col, dg, udg, g, res);
        g[u][i] = '.';
        col[i] = dg[u + i] = udg[n - u + i] = false;
      }
    }
  }
};
func solveNQueens(n int) [][]string {
  res := [][]string{}
  g := make([][]string, n)
  for i := range g {
    g[i] = make([]string, n)
    for j := range g[i] {
      g[i][j] = "."
    }
  }
  col := make([]bool, n)
  dg := make([]bool, 2*n)
  udg := make([]bool, 2*n)
  dfs(0, n, col, dg, udg, g, &res)
  return res
}

func dfs(u, n int, col, dg, udg []bool, g [][]string, res *[][]string) {
  if u == n {
    t := make([]string, n)
    for i := 0; i < n; i++ {
      t[i] = strings.Join(g[i], "")
    }
    *res = append(*res, t)
    return
  }
  for i := 0; i < n; i++ {
    if !col[i] && !dg[u+i] && !udg[n-u+i] {
      g[u][i] = "Q"
      col[i], dg[u+i], udg[n-u+i] = true, true, true
      dfs(u+1, n, col, dg, udg, g, res)
      g[u][i] = "."
      col[i], dg[u+i], udg[n-u+i] = false, false, false
    }
  }
}
using System.Collections.Generic;
using System.Text;

public class Solution {
  private IList<IList<string>> results = new List<IList<string>>();
  private int n;

  public IList<IList<string>> SolveNQueens(int n) {
    this.n = n;
    Search(new List<int>(), 0, 0, 0);
    return results;
  }

  private void Search(IList<int> state, int left, int right, int vertical)
  {
    if (state.Count == n)
    {
      Print(state);
      return;
    }
    int available = ~(left | right | vertical) & ((1 << n) - 1);
    while (available != 0)
    {
      int x = available & -available;
      state.Add(x);
      Search(state, (left | x ) << 1, (right | x ) >> 1, vertical | x);
      state.RemoveAt(state.Count - 1);
      available &= ~x;
    }
  }

  private void Print(IList<int> state)
  {
    var result = new List<string>();
    var sb = new StringBuilder(n);
    foreach (var s in state)
    {
      var x = s;
      for (var i = 0; i < n; ++i)
      {
        sb.Append((x & 1) != 0 ? 'Q': '.');
        x >>= 1;
      }
      result.Add(sb.ToString());
      sb.Clear();
    }
    results.Add(result);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文