From 872f4791dd9a9616c00202a2cbd1eedc1ac14d47 Mon Sep 17 00:00:00 2001 From: nilesh05apr Date: Wed, 5 Oct 2022 17:55:26 +0530 Subject: [PATCH] add: N-Queen Backtrack solution --- Java/LeetCodeN-Queens.java | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Java/LeetCodeN-Queens.java diff --git a/Java/LeetCodeN-Queens.java b/Java/LeetCodeN-Queens.java new file mode 100644 index 00000000..b29762b3 --- /dev/null +++ b/Java/LeetCodeN-Queens.java @@ -0,0 +1,67 @@ +/* Question Link: https://leetcode.com/problems/n-queens/ + * The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. + * Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. + * Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. + */ + + + + + + +class Solution { + public List> solveNQueens(int n) { + List> results = new ArrayList<>(); + if(n ==0) return results; + backtrace(results, new ArrayList(), 0, n, new boolean[n]); + return results; + } + + public static void backtrace(List> result, List list, int level, int n, boolean[] used){ + if(level == n) result.add(new ArrayList(list)); + else{ + for(int i = 0; i < n; i++){ + if(used[i]) continue; + if(isValid(list, level, i, n)){ + list.add(createQueen(n, i)); + used[i] = true; + backtrace(result, list, level + 1, n, used); + used[i] = false; + list.remove(list.size() - 1); + } + } + } + } + public static boolean isValid(List list, int row, int column, int n){ + if(row > 0){ + String cmp = list.get(row - 1); + for(int i = 0; i < row; i++) + if(list.get(i).charAt(column) == 'Q') return false; + int tempRow = row; + int tempCol = column; + while(--tempRow >= 0 && --tempCol >= 0){ + if(list.get(tempRow).charAt(tempCol) == 'Q') return false; + } + tempRow = row; + tempCol = column; + while(--tempRow >= 0 && ++tempCol <= n-1) + if(list.get(tempRow).charAt(tempCol) == 'Q') return false; + } + return true; + } + private static String createQueen(int n, int index){ + StringBuilder sb = new StringBuilder(); + int i = 0; + for(; i < index; i++) + sb.append('.'); + sb.append('Q'); + for(++i; i < n; i++){ + sb.append('.'); + } + return sb.toString(); + } + + +} + +/*contributed by: nilesh05apr */ \ No newline at end of file