java递归求八皇后问题(所有摆放方式)

来源:互联网 发布:java创建多线程数组 编辑:程序博客网 时间:2024/05/16 04:38

        简单说明一下思路。已知一个棋盘,棋子的放置范围为棋盘上的格子,另有四个限制条件:不同行,不同列,不同正斜线,不同反斜线。四个条件中,前两个条件比较简单,随便拿第一个作为先决条件,第二个为尝试条件,两个条件已经固定棋子的位置,所以第三个第四个就可认为是多余条件(就是为了提高问题难度而设置的条件);即放棋子时先看行,一行一个的放,在行中放置过程中尝试不同的列。在行放置过程中检查另外三个条件是否满足,满足就继续放,不满足就回退,尝试。

        前两个条件很好判断。在行从上往下递增列从左到右的坐标系下同一个正斜线上的点行和列的和相等。在行从上往下递增列从右到左递增的坐标系下同以反斜线上的点。和列的和相等。而同一个点的坐标在两个坐标系中很好计算(可以直接看出来)。

下面代码:

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Stack;class Point{int x,y;public Point(int x,int y ) {// TODO Auto-generated constructor stubthis.x=x;this.y=y;}public Point copy(){Point point=new Point(x, y);return point;}@Overridepublic String toString() {return x+","+y;}}public class EightQueen {static int startRow=0;static int endRow=7;public static void main(String[] args){List<Stack<Point>> list=new ArrayList<Stack<Point>>();Map<Integer, Integer> rowMap=new HashMap<Integer, Integer>();Map<Integer, Integer> colMap=new HashMap<Integer, Integer>();Map<Integer, Integer> positiveMap=new HashMap<Integer, Integer>();Map<Integer, Integer> negtiveMap=new HashMap<Integer, Integer>();Stack<Point> stack=new Stack<Point>();placeQueen(list, stack, rowMap, colMap, positiveMap, negtiveMap, startRow, endRow);System.out.println(list.size());for (Stack<Point> stack2 : list) {System.out.println("-----------------------------------------");for (Point point : stack2) {System.out.println(point);}}}static int count=0;public static int placeQueen(List<Stack<Point>> list,Stack<Point> stack,Map<Integer, Integer> rowMap,Map<Integer, Integer> colMap,Map<Integer, Integer> positiveMap,Map<Integer, Integer> negtiveMap,int currentRow,int aimRow){for(int i=0;i<=aimRow;i++){Integer countRow = rowMap.get(currentRow);Integer countCol=colMap.get(i);Integer countP=positiveMap.get(currentRow+i);Integer countN=negtiveMap.get(currentRow+aimRow-i);if(countRow==null&&countCol==null&&countP==null&&countN==null){rowMap.put(currentRow, 1);colMap.put(i, 1);positiveMap.put(currentRow+i, 1);negtiveMap.put(currentRow+aimRow-i, 1);if(currentRow==aimRow){Point point=new Point(currentRow, i);stack.push(point);Stack<Point> stack2=new Stack<Point>();for(int j=0;j<stack.size();j++){stack2.add(stack.get(j).copy());}list.add(stack2);stack.pop();rowMap.put(currentRow, null);colMap.put(i, null);positiveMap.put(currentRow+i, null);negtiveMap.put(currentRow+aimRow-i, null);return 0;}Point p=new Point(currentRow, i);stack.push(p);int result=placeQueen(list, stack,rowMap,  colMap, positiveMap, negtiveMap, currentRow+1, aimRow);if(result<1){stack.pop();rowMap.put(currentRow, null);colMap.put(i, null);positiveMap.put(currentRow+i, null);negtiveMap.put(currentRow+aimRow-i, null);}}}return -1;}}

0 0