Leetcode: N-Queens
来源:互联网 发布:数据库介绍ppt 编辑:程序博客网 时间:2024/06/05 14:52
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.."]]
vector<vector<string>> res;void permutationHelper(vector<int> &nums, int index, int n){if(index == n){bool flag = true;for(int i = 0; i<n && flag; i++)for(int j=i+1; j<n; j++)if((j-i) == abs(nums[j]-nums[i])){flag = false;break;}if(flag){vector<string> tmp;for(int i =0; i<n;i++){string row(n,'.');row[nums[i]]='Q';tmp.push_back(row);}res.push_back(tmp);}}for(int i = index; i < n; i++){swap(nums[index], nums[i]);permutationHelper(nums, index+1, n);swap(nums[index], nums[i]);}}void permutation(int n){vector<int> nums(n);for(int i = 0; i < n; i++)nums[i] = i;permutationHelper(nums,0,n);}vector<vector<string> > solveNQueens(int n) { // Note: The Solution object is instantiated only once. res.clear();permutation(n);return res; }
- 【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
- 【leetcode】N-Queens
- LeetCode: N Queens II
- [LeetCode]N-Queens
- [LeetCode]N-Queens II
- [leetcode]N-Queens
- [LeetCode] Anagrams、N-Queens
- Notepad++ 是程序员的必备利器之一
- 07-MyBatis_sql标签和include标签的使用
- 最大子矩阵求和问题
- 基于distanceTransform-距离变换的手掌中心提取
- 移植EMCV到DM6467(2)——OpenCV程序调试
- Leetcode: N-Queens
- eclipse 如何让c++不报错
- SDP协议及其应用
- ubunto用户切换
- 智能搜索算法--从A*算法开始说起
- 笔试回顾
- 华人及一季度和共和党人捷易通
- ACM题目:魔术数 M=K*Mc ,Mc为M的变形 比如M=102564,Mc=25641,102564=4*25641。
- android 中的 canvas , paint