Leetcode N-Queens I
来源:互联网 发布:尚雯婕的法语水平 知乎 编辑:程序博客网 时间:2024/06/07 14:52
N-Queens
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.."]]
写非递归回溯法要点:
1 记录好是否发生递归状态了
2 递归之后不能重复之前试过的解决方案
本题利用一个vector<int> backtrackCol(n,-1)实现了这两个功能
3 清楚什么时候需要回溯
本题是if (col == n) 这个条件就回溯了
4 回溯,就需要重置填过了的内容
本题需要重置两个数组: vector<int> backtrackCol和board
Iterative算法大概比recurrence算法难度高上三倍吧,而效率并没有明显区别。
下面是非递归算法12 queens用时也是12秒左右,和递归没什么区别。
可以打印的程序在下面连接:
http://blog.csdn.net/kenden23/article/details/14455915
vector<vector<string> > solveNQueens(int n) {vector<string> board(n,string(n, '.'));vector<vector<string> > rs;int row = 0;vector<int> backtrackCol(n, -1);while (row >= 0){int col = 0;if (backtrackCol[row] != -1){col = backtrackCol[row]+1;board[row][col-1] = '.';}for (; col < n; col++){if (isLegal(board, row, col)){board[row][col] = 'Q';backtrackCol[row] = col;if (row == n-1) {rs.push_back(board);backtrackCol[row] = -1;board[row][col] = '.';row--;break;}row++;break;}}if (col == n){backtrackCol[row] = -1;row--;}}return rs;}bool isLegal(const vector<string> &board, int row, int col){for (int i = 0; i < row; i++){if (board[i][col] == 'Q') return false;}for (int i = row - 1, j = col - 1; i>=0 && j>=0; i--, j--){if (board[i][j] == 'Q') return false;}for (int i = row - 1, j = col + 1; i>=0 && j<board.size(); i--, j++){if (board[i][j] == 'Q') return false;}return true;}
下面是递归回溯法,比上面简单多了。
vector<vector<string> > solveNQueens(int n) {vector<string> board(n,string(n, '.'));vector<vector<string> > rs;solNQueens(rs, board);return rs;}void solNQueens(vector<vector<string> > &rs, vector<string> &board, int row=0){if (row == board.size()) rs.push_back(board);for (int col = 0; col < board.size(); col++){if (isLegal(board, row, col)){board[row][col] = 'Q';solNQueens(rs, board, row+1);board[row][col] = '.';}}}bool isLegal(const vector<string> &board, int row, int col){for (int i = 0; i < row; i++){if (board[i][col] == 'Q') return false;}for (int i = row - 1, j = col - 1; i>=0 && j>=0; i--, j--){if (board[i][j] == 'Q') return false;}for (int i = row - 1, j = col + 1; i>=0 && j<board.size(); i--, j++){if (board[i][j] == 'Q') return false;}return true;}
递归回溯:
class Solution128 {public:vector<vector<string> > solveNQueens(int n) {vector<vector<string> > rs;vector<string> s(n, string(n, '.'));solve(rs, s, n);return rs;}void solve(vector<vector<string> > &rs, vector<string> &s, int n, int r = 0){if (r == n){rs.push_back(s);return;}for (int c = 0; c < n; c++){if (isLegal(s, r, c)){s[r][c] = 'Q';solve(rs, s, n, r+1);s[r][c] = '.';}}}bool isLegal(vector<string> &s, int r, int c){for (int i = r - 1, j = c - 1, k = c + 1; i >= 0 ; i--, j--, k++){if (s[i][c] == 'Q' || j>=0 && s[i][j] == 'Q' || k < s.size() && s[i][k] == 'Q')return false;}return true;}};
- Leetcode N-Queens I
- [Leetcode] N Queens I,II
- LeetCode - N-Queens I && II
- LeetCode | N-Queens I,II
- LeetCode题解:N-Queens I and II
- [LeetCode]N-Queens I and II
- [Leetcode 51, hard] N-Queens I
- LeetCode 51/52. N-Queens i, ii
- leetcode N-Queens I & II N皇后问题
- [LeetCode 51&52] N-Queens I & II (N皇后问题)
- LeetCode:N-Queens I (n皇后问题)
- leetcode -- N-Queens I&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
- 放假
- 小强的HTML5移动开发之路(12)——从一个多媒体标签说起
- Ubuntu下打开chrome提示 Your profile could not be opened correctly
- struts2输出html防止转码
- JBoss 系列八十五: JBoss Modules 简单介绍
- Leetcode N-Queens I
- Redis数据持久化机制AOF原理分析一
- IK中文分词器简介
- ubuntu12.04 恢复终端下命令TAB键补齐
- JAVA配置文件web.config【load-on-startup配置项解析】
- 打印 RichTextBox
- 学习 android ndk 1 - 相关配置与环境搭建
- CentOS 6.3 安装 MySQL 5.6.15并修改MySQL的root用户密码
- python抓取中文网页乱码