LeetCode 51. N-Queens

來源:互聯網
上載者:User

標籤:

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

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.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[ [".Q..",  // Solution 1  "...Q",  "Q...",  "..Q."], ["..Q.",  // Solution 2  "Q...",  "...Q",  ".Q.."]]

#include <iostream>#include <vector>#include <string>using namespace std;vector<string> queensLayout(vector< vector<int> >& chessBoard) {  vector<string> res;  for(int i = 0; i < chessBoard.size(); ++i) {    string tmp = "";    for(int j = 0; j < chessBoard[i].size(); ++j) {      if(chessBoard[i][j] == 1) tmp = tmp + "Q";      else tmp += ".";    }    res.push_back(tmp);  }  return res;}bool checkRows(vector< vector<int> >& chessBoard, int i, int dep) {  int tmp = 0;  // just need to check until the dep-th column  for(int k = 0; k < dep; ++k) {    tmp += chessBoard[i][k];  }  return tmp == 0;}bool checkCols(vector< vector<int> >& chessBoard, int i, int dep) {  int tmp = 0;  // just need to check until the i-th row  for(int k = 0; k < i; ++k) {    tmp += chessBoard[k][dep];  }  return tmp == 0;}bool checkDiag(vector< vector<int> >& chessBoard, int i, int dep) {  int tmp = 0;  int row = i, col = dep;  // check left-up diag.  while(row >= 0 && col >= 0) {    tmp += chessBoard[row][col];    row--;    col--;  }  if(tmp > 1) return false;  tmp = 0;  row = i, col = dep;  // check left-down diag.  while(row < chessBoard.size() && col >= 0) {    tmp += chessBoard[row][col];    row++;    col--;  }  return tmp == 1;}bool noConflict(vector< vector<int> >& chessBoard, int i, int dep) {  return checkRows(chessBoard, i, dep) && checkCols(chessBoard, i, dep) && checkDiag(chessBoard, i, dep);}void solveQueens(vector< vector<int> >& chessBoard, vector< vector<string> >& res, int dep) {  if(dep == chessBoard.size()) {    vector<string> tmp = queensLayout(chessBoard);    res.push_back(tmp);    return;  }  for(int i = 0; i < chessBoard.size(); ++i) {    chessBoard[i][dep] = 1;    if(noConflict(chessBoard, i, dep)) {      solveQueens(chessBoard, res, dep+1);    }    chessBoard[i][dep] = 0;  }}vector< vector<string> > solveQueens(int n) {  vector< vector<int> > chessBoard(n, vector<int>(n, 0));  vector< vector<string> > res;  solveQueens(chessBoard, res, 0);  return res;}int main(void) {  vector< vector<string> > res = solveQueens(4);  for(int i = 0; i < res.size(); ++i) {    for(int j = 0; j < res[i].size(); ++j) {      cout << res[i][j] << endl;    }    cout << endl;  }}


LeetCode 51. N-Queens

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.