• N-Queens 1&2


    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

    Given an integer n, return all distinct solutions to the n-queens puzzle.

    Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

    For example,
    There exist two distinct solutions to the 4-queens puzzle:

    [
     [".Q..",  // Solution 1
      "...Q",
      "Q...",
      "..Q."],
    
     ["..Q.",  // Solution 2
      "Q...",
      "...Q",
      ".Q.."]
    ]
    solution:经典八皇后问题(这里是n),用递归算法解决
    ps:
    国际象棋中,皇后会攻击横行竖行和斜线的位置的棋子。楼主用的二维数组比较直观。
    package leetcode2;
    import java.util.*;
    public class N_QUEEN {
        public static ArrayList<String[]> Nqueen(int n){
            ArrayList<String[]> res=new ArrayList<String[]>();
            char[][] location=new  char[n][n];
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    location[i][j]='.';
                }
            }
            int count=0;
            Queens(res,n,0,count,location);
            System.out.print("	"+res.size()+"  
    ");
            return res;
            
        }
        public static void Queens(ArrayList<String[]> res,int n,int deep,int count,char[][] location1){
              if(n==0){
                String[] sub=new String[location1.length];
                for(int i=0;i<location1.length;i++){
                 sub[i]=new String(location1[i]);    
                 System.out.print("   "+sub[i]);
                }
                res.add(sub);            
                count++;
            //    System.out.print(count);
                return;
            }
            for(int y=0;y<location1.length;y++) {
            if(isvaild(deep,y,location1)){
                location1[y][deep]='Q';
                Queens(res,n-1,deep+1,count,location1);
                location1[y][deep]='.';
            }
            
            }
            
        }
        
        public static boolean isvaild(int y, int x,char[][] location) {
            // TODO Auto-generated method stub
            boolean flag=true;
           for(int i = 0; i < location.length; i++) {
                    if(location[i][y] == 'Q' || location[x][i] == 'Q') return false; 
                }
                //left diagonal
                for(int i = x, j = y; i >=0 && j>=0; j--, i--)
                    if(location[i][j] == 'Q') return false; 
                for(int i = x, j = y; i <location.length && j>=0; j--, i++)
                    if(location[i][j] == 'Q') return false; 
                return true;    
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
          System.out.println(Nqueen(4));
        }
    
    }



  • 相关阅读:
    bootstrap-treeview 父子节点的全选与取消全选
    Nginx 中 proxy_pass 的斜杠问题
    Nginx 安装 echo-nginx-module 模块
    Nginx 内置变量与正则
    SpringBoot 整合 FastDFS
    CentOS7 搭建 FastDFS 环境
    配置 Idea + EmmyLua插件开发环境
    SpringBoot 整合 RabbitMQ
    数据结构笔记-环形队列
    SpringBoot 通过自定义 Mybatis 拦截器,实现 SQL 的改写
  • 原文地址:https://www.cnblogs.com/joannacode/p/4395730.html
Copyright © 2020-2023  润新知