博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] N-Queens
阅读量:6146 次
发布时间:2019-06-21

本文共 2856 字,大约阅读时间需要 9 分钟。

The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.

The idea is also very simple. Starting from the first row, try each column. If it does not induce any attack, move on to the next row based on the configurations of the previous rows. Otherwise, backtrack to the current row and try another selection of the column position. Once we reach the last row, add the current setting to a vector<vector<string> >.

The code below is referenced to , which records the positions of the queens using a nice 1d array like a[row] = col to indicate there is a queen at (row, col).

The code is as follows.

1 class Solution { 2 public: 3     vector
> solveNQueens(int n) { 4 vector
> queens; 5 vector
colPos(n, 0); 6 solve(colPos, n, 0, queens); 7 return queens; 8 } 9 private:10 bool noAttack(vector
& colPos, int row, int col) {11 for (int r = row - 1, ld = col - 1, rd = col + 1; r >= 0; r--, ld--, rd++)12 if (colPos[r] == col || colPos[r] == ld || colPos[r] == rd)13 return false;14 return true;15 }16 vector
queenStr(vector
& colPos) {17 int n = colPos.size();18 vector
queen(n, string(n, '.'));19 for (int i = 0; i < n; i++)20 queen[i][colPos[i]] = 'Q';21 return queen;22 }23 void solve(vector
& colPos, int n, int row, vector
>& queens) {24 if (row == n) {25 queens.push_back(queenStr(colPos));26 return;27 }28 for (int col = 0; col < n; col++) {29 colPos[row] = col;30 if (noAttack(colPos, row, col))31 solve(colPos, n, row + 1, queens);32 }33 }34 };

Well, if you have solved N-Queens II, you may know that problem has a nice bit-manipulation solution (you may refer to ). In fact, that bit-manipulation idea can also be used to solve this problem. The code is as follows.

1 class Solution { 2 public: 3     vector
> solveNQueens(int n) { 4 vector
> queens; 5 vector
queen; 6 int limit = (1 << n) - 1; 7 solve(0, 0, 0, n, limit, queen, queens); 8 return queens; 9 }10 private:11 void solve(int hProj, int lProj, int rProj, int n, int limit, vector
& queen, vector
>& queens) {12 if (hProj == limit) {13 queens.push_back(queen);14 return;15 }16 int pos = ~(hProj | lProj | rProj);17 for (int i = 0; i < n; i++) {18 int p = (1 << i);19 if (pos & p) {20 string line(n ,'.');21 line[i] = 'Q';22 queen.push_back(line);23 solve(hProj | p, (lProj | p) << 1, (rProj | p) >> 1, n, limit, queen, queens);24 queen.pop_back();25 }26 }27 }28 };

 

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4614567.html

你可能感兴趣的文章
centos 下安装g++
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
类斐波那契数列的奇妙性质
查看>>
配置设置[Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured....
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
代码描述10313 - Pay the Price
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
vb sendmessage 详解1
查看>>
jquery用法大全
查看>>
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
网卡驱动程序之框架(一)
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>