Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- private int res=0;
- public int totalNQueens(int n) {
- int[][] board = new int[n][n];
- helper(board,0);
- return res;
- }
- public void helper(int[][] board, int colIndex){
- if(colIndex==board.length){
- res++;
- return;
- }
- for(int i=0;i<board.length;i++){
- if(validate(board, i, colIndex)){
- board[i][colIndex] = 1;
- helper(board, colIndex+1);
- board[i][colIndex] = 0;
- }
- }
- }
- public boolean validate(int[][] board, int x, int y){
- for(int i=0;i<board.length;i++){
- for(int j=0;j<y;j++){
- if(board[i][j]==1 && (x + j == y + i || x + y == i + j || x == i)){
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement