~cpp public class QueensProblem { // to run, prompt>java QueensProblem x public static void main(String[] args) { QueensProblem problem = new QueensProblem(Integer.parseInt(args[0])); long start = System.currentTimeMillis(); problem.locate(0); System.out.println("소요시간(ms) = " + String.valueOf(System.currentTimeMillis() - start)); System.out.println("가능한 해답 = " + String.valueOf(problem.getSuccessNum())); } public QueensProblem(int queensNum) { this.queensNum = queensNum; board = new Chessboard(queensNum); } public int getSuccessNum() { return successNum; } public boolean locate(int index) { if (index == queensNum) { ++successNum; // System.out.println("성공 " + String.valueOf(++successNum)); // System.out.println(board); return true; } Chessboard.PointList points = board.getAvailablePoints(); Point point = null; while ((point = points.nextPoint()) != null) { if(point.getY() == index + 1) return false; Queen queen = new Queen(point); board.locate(queen); if (locate(index + 1)) { // return true; board.rollback(); } else { board.rollback(); } } return false; } private Chessboard board; private int queensNum; private int successNum; }
~cpp import java.util.*; public class Chessboard { public Chessboard(int size) { this.size = size; queens = new Stack(); availablePointsStack = new Stack(); List availablePoints = new ArrayList(); for (int y = 0; y < size; ++y) for (int x = 0; x < size; ++x) availablePoints.add(new Point(x, y)); availablePointsStack.push(new PointList(availablePoints)); } public void locate(Queen queen) { List availablePoints = new ArrayList(); List points = ((PointList)availablePointsStack.peek()).getPoints(); Iterator pointsIter = points.iterator(); while (pointsIter.hasNext()) { Point point = (Point)pointsIter.next(); if (!queen.canAttack(point)) availablePoints.add(point); } queens.push(queen); availablePointsStack.push(new PointList(availablePoints)); } public void rollback() { queens.pop(); availablePointsStack.pop(); } public PointList getAvailablePoints() { return (PointList)availablePointsStack.peek(); } public String toString() { StringBuffer buff = new StringBuffer(); Iterator iter = queens.iterator(); while (iter.hasNext()) { Queen queen = (Queen)iter.next(); buff.append(queen.getPoint().toString()); buff.append("\n"); } return buff.toString(); } private int size; private Stack queens; private Stack availablePointsStack; public static class PointList { public PointList(List points) { this.points = points; index = 0; } public Point nextPoint() { Point point = null; if (index < points.size()) { point = (Point)points.get(index); ++index; } return point; } public List getPoints() { return points; } public String toString() { StringBuffer buff = new StringBuffer(); Iterator iter = points.iterator(); while (iter.hasNext()) { Point point = (Point)iter.next(); buff.append(point); buff.append("\n"); } return buff.toString(); } private int index; private List points; } }
~cpp public class Queen { public Queen(Point point) { this.point = point; } public boolean canAttack(Point p) { return point.equalsX(p) || point.equalsY(p) || point.isLocatedCrossly(p); } public Point getPoint() { return point; } private Point point; }
~cpp public class Point { public Point(int x, int y) { this.x = x; this.y = y; } public boolean equalsX(Point point) { return point.x == x; } public boolean equalsY(Point point) { return point.y == y; } public boolean isLocatedCrossly(Point point) { int diffX = Math.abs(point.x - x); int diffY = Math.abs(point.y - y); return diffX == diffY; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "(" + String.valueOf(x) + ", " + String.valueOf(y) + ")"; } private int x; private int y; }