zoukankan      html  css  js  c++  java
  • 算法题(八皇后)

    问题:

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法?

    代码:


    public class huanghou{
      private int[] column;//同栏是否有皇后,1表示有
      private int[] rup;//右上至左下是否有皇后
      private int[] lup;//左上至右下是否有皇后
      private int[] queen;//解答
      private int num;//解答编号
      public huanghou(){
        column=new int[8+1];
        rup=new int[(2*8)+1];
        lup=new int[(2*8)+1];
        for(int i=1;i<=8;i++)
          column[i]=0;
          for(int i=1;i<=(2*8);i++)
          rup[i]=lup[i]=0; //初始定义全部无皇后

          queen=new int[8+1];
      }

      public void backtrack(int i){
        if(i>8){
          showAnswer();
        }else{
          for(int j=1;j<=8;j++){
            if((column[j]==0)&&(rup[i+j]==0)&&(lup[i-j+8]==0)){
              queen[i]=j;
              column[j]=rup[i+j]=lup[i-j+8]=1;
              backtrack(i+1);
              column[j]=rup[i+j]=lup[i-j+8]=0;
            }
          }
        }
      }

      protected void showAnswer(){
        num++;
        System.out.println(" 解答"+num);

        for(int y=1;y<=8;y++){
          for(int x=1;x<=8;x++){
            if(queen[y]==x){
              System.out.print("Q");
            }else{
              System.out.print(".");
            }
          }
          System.out.println();
        }
      }

      public static void main(String[]args){
        huanghou h=new huanghou();
        h.backtrack(1);
      }
    }

    改进:

    public class huanghou {
        private int[] column;//同栏是否有皇后,1表示有
        private int[] rup;//右上至左下是否有皇后
        private int[] lup;//左上至右下是否有皇后
        private int[] queen;//解答
        private static int num;//解答编号
        
        public huanghou() {
            column = new int[8+1];
            rup  = new int[2*8];
            lup  = new int[2*8];
            for (int i = 1; i <= 8; i++) {
                column[i] = 0;
            }
            for (int i = 1; i <= 8; i++) {
                rup[i] = lup[i] = 0;
            }
            queen = new int[8+1];
        }
        
        private void backtrack(int i) {
            if(i>8) {
                num++;
                showAnswer();
            }else {
                for (int j = 1; j <= 8; j++) {
                    if((column[j] == 0)&&(rup[i+j-1] == 0)&&(lup[i-j+8] == 0)) {
                        queen[i] = j;//放置第i个皇后
                        column[j] = rup[i+j-1] = lup[i-j+8] = 1;//同栏,同斜线上不能再放置皇后
                        backtrack(i+1);//准备放置第i+1个皇后
                        column[j] = rup[i+j-1] = lup[i-j+8] = 0;//撤回第i个皇后
                    }
                }
            }
        }
        
    //    输出所有情况
        protected void showAnswer() {
            System.out.println("
    解答" + num);
            
            for (int i = 1; i <= 8; i++) {
                for (int j = 1; j <= 8; j++) {
                    if (queen[i] == j) {
                        System.out.print("Q");
                    }else {
                        System.out.print(".");
                    }
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            huanghou h=new huanghou();
            h.backtrack(1);
            System.out.println(num);//输出最终种类数
        }
    }
  • 相关阅读:
    CODEVS4650 破损的键盘
    洛谷P1656 炸铁路
    洛谷 P3225 [HNOI2012]矿场搭建
    1265 四点共面
    1406: [AHOI2007]密码箱
    1193: [HNOI2006]马步距离
    1800: [Ahoi2009]fly 飞行棋
    1923: [Sdoi2010]外星千足虫
    I
    2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
  • 原文地址:https://www.cnblogs.com/-rainbow-/p/7410917.html
Copyright © 2011-2022 走看看