回溯法求解八皇后问题


public class Queen {
    private static final boolean AVAILABLE = true;
    private int squares = 8, norm = squares - 1;
    private int positionInRow[] = new int[squares];
    private int p = -1;
    private boolean[] rows = new boolean[squares];
    private boolean[] column = new boolean[squares];
    private boolean[] leftDiagonal = new boolean[2 * squares - 1];
    private boolean[] rightDiagonal = new boolean[2 * squares - 1];
    private static int ct = 0;

    public Queen() {
        // To complete the initialization work for the
        // column,leftDiagonal,rigthDiagonal.
        for (int i = 0; i < squares; i++) {
            rows[i] = AVAILABLE;
            column[i] = AVAILABLE;
            positionInRow[i] = -1;
        }
        for (int i = 0; i < 2 * squares - 1; i++) {
            leftDiagonal[i] = AVAILABLE;
            rightDiagonal[i] = AVAILABLE;
        }

    }

    public void printResults(int[] columns) {
        int row, col;
        System.out.print("(");
        for (int e : columns) {
            System.out.print("  "+e);
        }
        
        System.out.print(")\n");
    }

    public void putQueen() {
        int row = 0, col;
        while (true) {
            for (col = p + 1; col < squares; col++) {
                if (rows[row] == AVAILABLE && column[col] == AVAILABLE
                        && leftDiagonal[row + col] == AVAILABLE
                        && rightDiagonal[row - col + norm] == AVAILABLE) {
                    break;
                }
            }
            // 在当前的行里面找到了可以放置皇后的位置
            if (col < squares) {
                rows[row] = !AVAILABLE;
                column[col] = !AVAILABLE;
                leftDiagonal[row + col] = !AVAILABLE;
                rightDiagonal[row - col + norm] = !AVAILABLE;
                positionInRow[row] = col;
                p = col;
            } else// 如果当前行没办反放置皇后了,那么回溯
            {
                if (row > 0)// 到前一行
                {
                    row--;
                    p = positionInRow[row];
                    rows[row] = AVAILABLE;
                    column[p] = AVAILABLE;
                    leftDiagonal[row + p] = AVAILABLE;
                    rightDiagonal[row - p + norm] = AVAILABLE;
                    positionInRow[row] = -1;
                    continue;
                } else {
                    break;
                }
            }
            if (row == squares - 1) {
                ct += 1;
                printResults(positionInRow);
                p = positionInRow[row];
                rows[row] = AVAILABLE;
                column[p] = AVAILABLE;
                leftDiagonal[row + p] = AVAILABLE;
                rightDiagonal[row - p + norm] = AVAILABLE;
                positionInRow[row] = -1;
                continue;
            } else {
                row++;
                p = -1;
                continue;
            }
        }
    }

    public static void main(String args[]) {
        Queen eightQueens = new Queen();
        eightQueens.putQueen();
        System.out.println("Count=" + ct);
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值