(Java)LeetCode-51. N-Queens
来源:互联网 发布:淘宝卖家装修教程 编辑:程序博客网 时间:2024/06/01 11:01
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 List<List<String>> solveNQueens(int n) {List<List<String>> result = new ArrayList<List<String>>();List<String> line = new ArrayList<String>();char[][] cube = new char[n][n];for(int i = 0; i < cube.length; i++){for(int j = 0; j < cube[i].length; j++){cube[i][j] = '.';}}boolean[] flags = new boolean[n];solveNQueens(result, cube, flags, n, 0); return result; }private void solveNQueens(List<List<String>> result, char[][] cube,boolean[] flags,int n, int cnt) {// TODO Auto-generated method stubif(cnt == n){List<String> list = new ArrayList<String>();for(char[] ch : cube){list.add(new String(ch));}result.add(list);return ;}for(int i = 0; i < flags.length; i++){if(flags[i] == false){List<Integer> temp = aPosition(cube, i);if(temp.size() == 0){break;}for(int t : temp){cube[i][t] = 'Q';flags[i] = true;solveNQueens(result, cube, flags, n , cnt+1);cube[i][t] = '.';flags[i] = false;}break;}}}private List<Integer> aPosition(char[][] cube, int num) {// TODO Auto-generated method stubList<Integer> result = new ArrayList<Integer>();int n = cube.length;boolean flag = false;for(int i = 0; i < n; i++){//the direction is "--"if(cube[num][i] == 'Q'){return result;}}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){//the direction is "|"if(cube[j][i] == 'Q'){flag = true;break;}}if(flag == false){if(num >= i){// the direction is "\"int dif = num - i;for(int j = 0; j + dif <= n-1; j++){ if(cube[j+dif][j] == 'Q'){flag = true;break;}}}else{int dif = i - num;for(int j = 0; j + dif <= n-1; j++){if(cube[j][j+dif] == 'Q'){flag = true;break;}}}if(flag == false){if(num + i <= n-1){//the direction is "/"int sum = i + num;for(int j = 0; sum >= j; j++ ){if(cube[j][sum-j] == 'Q'){flag = true;break;}}}else{int sum = i + num;for(int j = n-1; sum-j <= n-1; j--){if(cube[sum-j][j] == 'Q'){flag = true;break;}}}}}if(flag == false){result.add(i);}else{flag = false;}}return result;}public static void main(String[] args){ Solution sol = new Solution(); System.out.println("start"); List<List<String>> re = sol.solveNQueens(4); for(List<String> l : re){ System.out.println(l); } }
0 0
- (Java)LeetCode-51. N-Queens
- [Leetcode] N-Queens (Java)
- [LeetCode][Java] N-Queens
- leetcode:N-Queens 【Java】
- N-Queens leetcode java
- 【Leetcode】:51. N-Queens 问题 in JAVA
- LeetCode 51. N-Queens
- LeetCode --- 51. N-Queens
- LeetCode 51.N-Queens
- [Leetcode] 51. N-Queens
- [leetcode] 51.N-Queens
- [leetcode] 51. N-Queens
- LeetCode 51. N-Queens
- leetcode 51. N-Queens
- 【LeetCode】51. N-Queens
- leetcode 51. N-Queens
- LeetCode 51. N-Queens
- LeetCode 51. N-Queens
- javaweb里面的lib下面的包删不掉
- ZYNQ-Miz702 VIVADO 开发环境建立(第一章 )
- SearchView(简单的java写Menu)
- 解决myeclipse错误:Could not create the view: An unexpected exception was thrown.
- HTML学习笔记:1 HTML简介
- (Java)LeetCode-51. N-Queens
- 1001
- 周志华《机器学习》勘误表
- cat /proc/modules:查询当前系统被加载模块
- 最小路径覆盖
- 简析hashset的实现原理
- MVC的简单搭建之配置类以及日志类
- 数据结构学习--插入排序
- 重新学习c++ day01