leetcode 050 —— N-Queens
来源:互联网 发布:定海神针网络上啥意思 编辑:程序博客网 时间:2024/05/16 12:57
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.."]]
直接上代码
class Solution {public:vector<vector<string> > solveNQueens(int n) {//string s( n, '.' );vector<string> path(n, string (n, '.'));vector<vector<string> > res;create(0, n, path, res);return res;}bool isValid(int v, int s, int n, vector<string> &path){ //第v行的第s列 是否有效int vv, ss;for (int i = 0; i < v; i++){vv = i;ss = path[vv].find('Q');if (ss == s)return false;if ((v - vv) == (s - ss))return false;if ((v - vv) == (ss - s))return false;}return true;}void create(int level, int n, vector<string> &path, vector<vector<string> > &res){if (level == n){res.push_back(path);return;}for (int i = 0; i < n; i++){if (isValid(level, i, n, path)){path[level][i] = 'Q';create(level + 1, n, path, res);path[level][i] = '.';}}}};
0 0
- leetcode 050 —— N-Queens
- LeetCode—51.N-Queens
- LeetCode: N-Queens [050]
- LeetCode——N-Queens II
- LeetCode——N-Queens II
- leetcode 052 —— N-Queens II
- 【LeetCode】N-Queens && N-Queens II
- leetcode N-Queens & N-Queens II
- Leetcode: N-Queens && N-Queens II
- 【Leetcode】【python】N-Queens/N-Queens II
- LeetCode: N-Queens II
- LeetCode: N-Queens
- LeetCode : N-Queens
- LeetCode : N-Queens II
- [Leetcode] N-Queens II
- leetcode 72: N-Queens
- LeetCode 38: N-Queens
- 【leetcode】N-Queens II
- Servlet
- json数组 json对象
- 网页动画的十二原则
- 排序讲解之快排
- Collective Intelligence
- leetcode 050 —— N-Queens
- Online Judge System For SzNOI 题库 语法百题 C++ d015
- 策略设计模式
- 迷宫算法及数据结构分析(by WIzaRD_ssc)
- 后剪枝之悲观剪枝法
- istview下拉刷新 上拉(滑动分页)加载更多
- TCP详解
- 从SVN上down下来的maven项目,无法下载jar包
- Python 时间,日期,时间戳(一)