LeetCode N-Queens
2015-07-31 19:30
447 查看
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.."] ]
利用回朔法
public class Solution { public List<List<String>> solveNQueens(int n) { //棋盘,0表示干净,n表示放有皇后n,负数表示是有多少个皇后将这个格子据为己有 int[][] chessboard = new int ; List<List<String>> result = new ArrayList<List<String>>(); place(1,chessboard,result); return result; } /* * 将第n个皇后放到棋盘上 */ public static void place(int n,int[][] chessboard,List<List<String>> result){ for(int j = 0; j < chessboard.length ; j++){ //格子是否干净 if(chessboard[n-1][j] == 0){ //是否是最后一个皇后 if(n == chessboard.length){ chessboard[n-1][j] = n; result.add(printChessboard(chessboard)); chessboard[n-1][j] = 0; //break; }else{ //将皇后n放到棋盘格子[n-1][j]上 chessboard[n-1][j] = n; occupy(n-1,j,chessboard); place(n+1,chessboard,result); chessboard[n-1][j] = 0; clear(n-1,j,chessboard); } } } } //占地盘 public static void occupy(int row , int col,int[][] chessboard){ int n = chessboard.length; int i = 0; int j = 0; //将同一行据为己有 for(j = 0; j < n ; j ++){ if(j != col){ chessboard[row][j]--; } } //将同一列据为己有 for(i = 0; i < n ; i ++){ if(i != row){ chessboard[i][col]--; } } //将左上斜线据为己有 i = row - 1; j = col - 1; while(i>=0 && j>= 0){ chessboard[i][j]--; i--; j--; } i = row + 1; j = col + 1; //将右下斜线据为己有 while(i < n && j < n){ chessboard[i][j]--; i++; j++; } //将右上斜线 i = row - 1; j = col + 1; while(i>=0 && j < n){ chessboard[i][j]--; i--; j++; } //将左下斜线 i = row + 1; j = col - 1; while(i < n && j >= 0){ chessboard[i][j]--; i++; j--; } } //清理地盘 public static void clear(int row , int col,int[][] chessboard){ int n = chessboard.length; int i = 0; int j = 0; //清理同一行 for(j = 0; j < n ; j ++){ if(j != col){ chessboard[row][j]++; } } //清理同一列 for(i = 0; i < n ; i ++){ if(i != row){ chessboard[i][col]++; } } //清理左上斜线 i = row - 1; j = col - 1; while(i>=0 && j>= 0){ chessboard[i][j]++; i--; j--; } i = row + 1; j = col + 1; //清理右下斜线 while(i < n && j < n){ chessboard[i][j]++; i++; j++; } //将右上斜线 i = row - 1; j = col + 1; while(i>=0 && j < n){ chessboard[i][j]++; i--; j++; } //将左下斜线 i = row + 1; j = col - 1; while(i < n && j >= 0){ chessboard[i][j]++; i++; j--; } } public static List<String> printChessboard(int[][] chessboard){ List<String> list = new ArrayList<String>(); for(int i = 0; i < chessboard.length; i++){ StringBuffer buffer = new StringBuffer(); for(int j = 0; j < chessboard[0].length; j++){ if(chessboard[i][j] > 0){ buffer.append('Q'); }else{ buffer.append('.'); } } list.add(buffer.toString()); } return list; } }
相关文章推荐
- UIImage存为本地文件,UIImage转换为NSData
- Equivalent Strings(Codeforces Round #313 (Div. 2) D)
- Snail—UI学习之UITableView之自定义UITableViewCell
- EasyUI----DataTable转拼EasyUI Tree
- Android UI列表的上拉下拉刷新之Android-PullToRefresh
- Snail—UI学习之UITableView之分组显示
- 【读书笔记】UIFont-动态下载系统提供的字体-官方代码
- 【读书笔记】UIFont-动态下载系统提供的字体-官方代码
- Field 'ssl_cipher' doesn't have a default value
- POJ_1458_CommonSubsequence
- vue.js学习
- UIImageView动画
- 关于DUILIB的遮罩,控件穿透以及其应用的分析实践
- Snail—UI学习之表视图TableView多行删除
- OC 中的关键词 大全(1)
- 使用camare GPUImage遇到的问题
- 用easyUI的combobox实现多选下拉框
- ios UICollectionView 的使用
- iOS学习开发2:UIView和UIWindow
- EasyUI实现多级联动