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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器