【一天一道LeetCode】#51. N-Queens
2016-05-17 14:40
573 查看
一天一道LeetCode系列
(一)题目
The n-queens puzzle is the problem of placing n queens on an n×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..”]
]
(二)解题
不玩国际象棋还真不知道这题的规则。百度了好久才明白。主要有以下三个:
+ 同一行上只能有一个皇后
+ 同一列上只能有一个皇后
+ 两个皇后之间不能处在同一条对角线上
具体解法看代码:
/* 首先利用一个数row和数组a[i]确保每一行每一列只有一个皇后 然后利用一个vector存储已摆放皇后的位置坐标,每摆一个皇后就与已摆放的皇后进行比较,如果在一条对角线上就不能摆 */ class Solution { public: vector<vector<string>> ret; vector<pair<int, int>> queens;//存放已摆放的皇后的坐标值 vector<vector<string>> solveNQueens(int n) { int *a = new int ;//确保每一列只有一个皇后 memset(a,0,n*sizeof(int)); vector<string> res; backtrc(res, a, 0, n); return ret; } bool isValid(vector<pair<int,int>> queens , int row,int col)// { if (queens.empty()) return true; for (int i = 0; i < queens.size();i++) { if (abs(row- queens[i].first) == abs(col-queens[i].second)) { return false; } } return true; } void backtrc(vector<string> res, int a[], int row, int n)//row确保每一行只有一个皇后 { if (row == n)//如果摆放完n行,则退出 { ret.push_back(res); return; } for (int i = 0; i < n; i++) { if (a[i] == 0&&isValid(queens, row, i))//保证了同一行,同一列,同一对角线只有一个Q { a[i] = 1; string tmp(n, '.'); tmp[i] = 'Q'; res.push_back(tmp); queens.push_back(pair<int, int>(row, i)); backtrc(res, a, row + 1, n); //回溯 a[i] = 0; queens.pop_back(); res.pop_back(); } } } };
相关文章推荐
- 1.UIAlertController
- hdu3436 Queue-jumpers(Splay)
- UIProgressView进度条
- BlockingQueue的使用
- UISegmentedControl的用法
- druid1.0.19使用注意事项
- UINavgationController中覆写preferredStatusBarStyle方法不执行的问题
- 如果在Scope中的Preview中发起一个query请求
- EXC_BAD_ACCESS on UIAlertview code=1
- 25个优秀的UI设计资源下载
- java GUI 图形界面编程
- UIBezierPath精讲(二)
- mui 浏览器跨域实现
- 304. Range Sum Query 2D - Immutable
- 2016 UESTC Training for Dynamic Programming Q - 柱爷的宝藏 斜率优化
- 2016 UESTC Training for Dynamic Programming P - 柱爷的矩阵 矩阵、递推
- 2016 UESTC Training for Dynamic Programming N - 柱爷与子序列 这题和N题有些相似之处、用了树状数组
- 2016 UESTC Training for Dynamic Programming M - 柱爷抢银行欢庆5.1special 递推
- pdo中query()与prepare().execute()
- 2016 UESTC Training for Dynamic Programming L - 柱爷抢银行MkⅣ dp 线段树优化