Leetcode N-Queens系列
来源:互联网 发布:深圳行知美术学校招聘 编辑:程序博客网 时间:2024/04/30 03:07
#51 N-Queens
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.."]]
经典的回溯问题,为了减少对每个方格是否可放置皇后的判断,提前用一个数组记录下来。
代码中 isvalid 不同下标范围具有不同的含义:
0 ~ n- 1 index = col 代表 第 col 列的状态(共n列)
n ~ 3n-2 index = n + row + col 代表(row,col)所在的正斜线的状态(共2n -1个正斜线)
3n -1 ~ 5n -3 index = 3n - 2 + n -row + col 代表(row, col)所在反斜线的状态(共2n -1个反斜线)
class Solution {public: vector< vector<string> > solveNQueens(int n) { vector< vector<string> > res; if (n == 0) return res; int row = 0; vector<int> isvalid(n + 2 * (2 * n - 1), 1); vector<int> col(n, -1); while(row >= 0){ if(col[row] != -1) { isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 1; } col[row] ++; while(col[row] < n){ if(isvalid[col[row]] && isvalid[n + row + col[row]] && isvalid[3 * n - 2 + n - row + col[row]] ) break; col[row] ++; } if(col[row] == n) row--; //回溯 else if(row == n -1){ vector<string> one; for(int i = 0; i < n; i ++){ string s = ""; s.insert(0, n - 1, '.'); s.insert(col[i], 1, 'Q'); one.push_back(s); } res.push_back(one); } else{ isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 0; col[++row] = -1; } } return res; }};
#52 N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
由上面代码稍加修改即可:
class Solution {public: int totalNQueens(int n) { if (n == 0) return 0; int res = 0, row = 0; vector<int> isvalid(n + 2 * (2 * n - 1), 1); vector<int> col(n, -1); while(row >= 0){ if(col[row] != -1) { isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 1; } col[row] ++; while(col[row] < n){ if(isvalid[col[row]] && isvalid[n + row + col[row]] && isvalid[3 * n - 2 + n - row + col[row]] ) break; col[row] ++; } if(col[row] == n) row--; //回溯 else if(row == n -1) res ++; else{ isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 0; col[++row] = -1; } } return res; }};
可以写成递归形式如下:
class Solution {public: void dfs(int &res, vector<int> &isvalid, int row, int n) { if (row == n){ ++res; return; } for (int col = 0; col < n; col++) { if (isvalid[col] && isvalid[n + row + col] && isvalid[3 * n - 2 + n - row + col]){ isvalid[col] = isvalid[n + row + col] = isvalid[3 * n - 2 + n - row + col] = 0; dfs(res, isvalid, row + 1, n); isvalid[col] = isvalid[n + row + col] = isvalid[3 * n - 2 + n - row + col] = 1; } } return; } int totalNQueens(int n) { if (n == 0) return 0; int res = 0; vector<int> isvalid(n + 2 * (2 * n - 1), 1); dfs(res, isvalid, 0, n); return res; }};
- Leetcode N-Queens系列
- leetcode系列(60)N-Queens, N-Queens II
- [leetcode刷题系列]N-Queens
- [leetcode刷题系列]N-Queens 2
- 【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
- 数字图像学习4(图像的浮雕效果)
- HDU 4907 Task schedule
- MFC几种给对话框添加背景图的方法
- 【黑马程序员】ARC概念及原理
- RadioButton使用方法
- Leetcode N-Queens系列
- android分辨率 尺寸 dpi换算
- LotusScript获取一个视图中所有文档
- 用户名和密码自动清空
- Removing Columns
- 代理服务器的理解(1):Windows环境下的代理服务器设置
- Delete Node in a Linked List
- LotusScript判断文档是否存在
- HDU 5285