LAREINA.JO 2021-04-03 17:41 采纳率: 60%
浏览 69
已采纳

关于java 八皇后的问题

之前的帖子也有发过。这一次让重新写这个作业,要求是一样的。还是没太搞明白,求大神支援。要求在下面,要读要求,必须是二位char array并且method必须按照他的要求写十分感谢!

• Name the class EightQueens

• Make all the required methods public

• Represent the chessboard as an instance variable, a two-dimensional char array. Use the char Q for a queen and o (lower case letter O) for an empty space

• The class must support a deep copy through a method called clone()

• When an object is created, it should return an empty chessboard (that is, one filled with o)

• getBoard() returns the current state of the chessboard

• setQueen(int row, int column) marks the square as Q

• emptySquare(int row, int column) marks the square as o

• setQueens(int queensRemaining) returns boolean (success or failure of placement). It sets the specified number of queens in allowed positions on the board. Note that the board may already have queens placed on it

• The solution should employ recursion

• Placement of the queens must be calculated by your program, rather than hardcoded

  • 写回答

2条回答 默认 最新

  • ProfSnail 2021-04-03 20:12
    关注
    /**
     @author Rui Guan <a href="mailto:rui.guan@ucalgary.ca">
     rui.guan@ucalgary.ca</a>
     @version 1.9
     @since 1.0
     */
    /*
    This is a program which can place any number of queens, up to a maximum of 8, on a
     an 8x8 grid chessboard such that no queen can attack any other queens.
    */
    
    
    
    public class EightQueens implements Cloneable{
    
        @Override
        public EightQueens clone() throws CloneNotSupportedException {
            // TODO Auto-generated method stub
            return (EightQueens) super.clone();
        }
    
        public static void main(String[] args) {
            EightQueens a=new EightQueens();
            System.out.println(a.setQueens(6));
            a.print();
            System.out.println(a.setQueens(1));
            a.print();
        }
    
    
        private char chessboard[][];
        private boolean queens[];
        private int queenNum;
        //constructor
        public EightQueens() {
            //the new object is empty
            chessboard=new char[8][8];
            queens = new boolean[8];
            queenNum = 0;
            for(int i=0;i<8;i++) {
                queens[i] = false;
                for(int j=0;j<8;j++) {
                    emptySquare(i,j);
                }
            }
        }
    
    
        //method getBoard
        public char[][] getBoard() {
            return chessboard;
        }
    
        //method setQueen: put Q into the square
        public void setQueen (int row, int column) {
            chessboard[row][column]='Q';
        }
    
        //method emptySquare: put o into empty square
        public void emptySquare(int row, int column){
            chessboard[row][column]='o';
        }
    
    
        //method setQueens: put the specific amount of chess into it
        public boolean setQueens(int queensRemaining){
            // 少于0了,不行
            if(queensRemaining < 0) {
                return false;
            }
            // 已有的皇后数目和需要摆放的皇后数目,加起来超过8了,不行
            if(queenNum + queensRemaining > 8){
                return false;
            }
            // 只需要放0个皇后,可以的
            if(queensRemaining == 0){
                return true;
            }
            // 看看哪儿有空地方
            for(int i = 0; i < 8; i ++){
                if(queens[i])continue;
                for(int j = 0; j < 8; j++){
                    boolean flag = rule(i, j);
                    if(flag){
                        // 能放就放
                        setQueen(i, j);
                        queenNum++;
                        queens[i] = true;
                        // 看看其他位置还能不能放
                        boolean flag2 = setQueens(queensRemaining-1);
                        if(flag2){
                            // 能放,得嘞
                            return true;
                        }
                        else{
                            // 不能放,拉倒
                            queenNum--;
                            queens[i] = false;
                            emptySquare(i, j);
                        }
                    }
                }
            }
            return false;
        }
    
    
        // check if the space is available for queens
        public boolean rule(int u,int v){
            for(int i = 0; i < 8; i++) {
                // 同行是冤家
                if (chessboard[i][v] == 'Q') {
                    return false;
                }
                // 同列也打架
                if (chessboard[u][i] == 'Q') {
                    return false;
                }
            }
            int i = u, j = v;
            while(i >= 0 && i < 8 && j >= 0 && j < 8){
                // 主对角线往上找
                if(chessboard[i][j]=='Q')return false;
                i--;
                j--;
            }
            i = u;
            j = v;
            while(i >= 0 && i < 8 && j >= 0 && j < 8){
                // 主对角线往下找
                if(chessboard[i][j]=='Q')return false;
                i++;
                j++;
            }
            i = u;
            j = v;
            while(i >= 0 && i < 8 && j >= 0 && j < 8){
                // 副对角线往上找
                if(chessboard[i][j]=='Q')return false;
                i++;
                j--;
            }
            i = u;
            j = v;
            while(i >= 0 && i < 8 && j >= 0 && j < 8){
                // 副对角线往下找
                if(chessboard[i][j]=='Q')return false;
                i--;
                j++;
            }
            return true;
        }
    
    
    
        //This is just for test
        public void print(){
            for(int i=0;i<8;i++){
                for(int m=0;m<8;m++){
                    System.out.print(chessboard[i][m]+" ");
                }
                System.out.print("\n");
            }
            System.out.print("\n");
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向
  • ¥30 sort cuteSV.vcf by bcftools用IGV可视化出现报错
  • ¥100 SOS!对STK中导出的天体图像进行质心提取有没有人做过啊
  • ¥15 python 欧式距离
  • ¥15 运行qteasy报错
  • ¥15 遗传算法解决有工序顺序约束的大规模FJSP问题
  • ¥15 企业消防水炮塔设计方案
  • ¥20 WORKBENCH网格划分