zoukankan      html  css  js  c++  java
  • 51. N-Queens

    class Solution {
        public List<List<String>> solveNQueens(int n) {
            int[][] board=new int[n][n];
            List<List<String>> res=new ArrayList<List<String>>();
            nQueens(0,board,res);
            return res;
        }
        private void nQueens(int i, int[][] board, List<List<String>> res){
            if(i==board.length)
            {
                List<String> list=new ArrayList<String>();
                for(int y=0;y<board.length;y++)
                {
                    String row="";
                    for(int x=0;x<board.length;x++)
                        row+=board[y][x]==1?'Q':'.';
                    list.add(row);
                }
                res.add(list);
                return;
            }
            for(int j=0;j<board.length;j++)
                if(board[i][j]==0)
                {
                    board[i][j]=1;
                    fillQueuePath(i, j, board, -1);
                    nQueens(i+1,board,res);
                    fillQueuePath(i, j, board, 1);
                    board[i][j]=0;
                }
        }
        private void fillQueuePath(int i, int j, int[][] board, int val){
            for(int k=1;i+k<board.length;k++)
            {
                board[i+k][j]+=val;
                if(j-k>=0)
                    board[i+k][j-k]+=val;
                if(j+k<board.length)
                    board[i+k][j+k]+=val;
            }
        }
    }
    class Solution {
        public List<List<String>> solveNQueens(int n) {
            List<List<String>> res=new ArrayList<List<String>>();
            boolean[] cols=new boolean[n];
            boolean[] d1=new boolean[n*2];
            boolean[] d2=new boolean[n*2];
            solveNQueens(0, cols, d1, d2, new ArrayList<String>(), res);
            return res;
        }
        private void solveNQueens(int i, boolean[] cols, boolean[] d1, boolean[] d2, List<String> list, List<List<String>> res)
        {
            if(i==cols.length)
            {
                res.add(new ArrayList<String>(list));
                return;
            }
            for(int j=0;j<cols.length;j++)
            {
                int dn1=i-j+cols.length;
                int dn2=i+j;
                if(cols[j]==false&&d1[dn1]==false&&d2[dn2]==false)
                {
                    cols[j]=true;
                    d1[dn1]=true;
                    d2[dn2]=true;
                    String row="";
                    for(int k=0;k<cols.length;k++)
                        row+=k==j?'Q':'.';
                    list.add(row);
                    solveNQueens(i+1,cols, d1, d2, list, res);
                    list.remove(list.size()-1);
                    cols[j]=false;
                    d1[dn1]=false;
                    d2[dn2]=false;
                }
            }
        }
    }
  • 相关阅读:
    网络编程
    模块
    内置函数
    函数应用
    万能参数
    函数
    爬虫
    算法
    Flask
    linux
  • 原文地址:https://www.cnblogs.com/asuran/p/7591343.html
Copyright © 2011-2022 走看看