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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么