Advertisement
eulaliaaires

NQueens

May 25th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /* Java program to solve N Queen Problem using
  4.    backtracking */
  5. public class NQueenProblem {
  6.     final int N = 8;
  7.     static int k = 1;
  8.     int output[] = new int[N];
  9.  
  10.     /* A utility function to print solution */
  11.     void printSolution(int board[][], int linha, int coluna) {
  12.         if (board[linha - 1][coluna - 1] == 1) {
  13.             System.out.print(" " + k++ + "      ");
  14.             for (int j = 0; j < N; j++) {
  15.                 for (int i = 0; i < N; i++) {
  16.                     if (board[i][j] == 1) {
  17.                         System.out.print((i+1) + " ");
  18.                         break;
  19.                     }
  20.                 }
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25.  
  26.     /*
  27.      * A utility function to check if a queen can be placed on board[row][col]. Note
  28.      * that this function is called when "col" queens are already placeed in columns
  29.      * from 0 to col -1. So we need to check only left side for attacking queens
  30.      */
  31.     boolean isSafe(int board[][], int row, int col) {
  32.         int i, j;
  33.  
  34.         /* Check this row on left side */
  35.         for (i = 0; i < col; i++)
  36.             if (board[row][i] == 1)
  37.                 return false;
  38.  
  39.         /* Check upper diagonal on left side */
  40.         for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
  41.             if (board[i][j] == 1)
  42.                 return false;
  43.  
  44.         /* Check lower diagonal on left side */
  45.         for (i = row, j = col; j >= 0 && i < N; i++, j--)
  46.             if (board[i][j] == 1)
  47.                 return false;
  48.  
  49.         return true;
  50.     }
  51.  
  52.     /*
  53.      * A recursive utility function to solve N Queen problem
  54.      */
  55.     boolean solveNQUtil(int board[][], int col, int linha, int coluna) {
  56.         /*
  57.          * base case: If all queens are placed then return true
  58.          */
  59.         if (col == N) {
  60.             printSolution(board, linha, coluna);
  61.             return true;
  62.         }
  63.         boolean res = false;
  64.         /*
  65.          * Consider this column and try placing this queen in all rows one by one
  66.          */
  67.         for (int i = 0; i < N; i++) {
  68.             /*
  69.              * Check if queen can be placed on board[i][col]
  70.              */
  71.             if (isSafe(board, i, col)) {
  72.                 /* Place this queen in board[i][col] */
  73.                 board[i][col] = 1;
  74.                 res = solveNQUtil(board, col + 1, linha, coluna) || res;
  75.  
  76.                 /* recur to place rest of the queens */
  77.                 if (solveNQUtil(board, col + 1, linha, coluna) == true)
  78.                     return true;
  79.  
  80.                 /*
  81.                  * If placing queen in board[i][col] doesn't lead to a solution then remove
  82.                  * queen from board[i][col]
  83.                  */
  84.                 board[i][col] = 0; // BACKTRACK
  85.             }
  86.         }
  87.  
  88.         /*
  89.          * If queen can not be place in any row in this colum col, then return false
  90.          */
  91.         return res;
  92.     }
  93.  
  94.     /*
  95.      * This function solves the N Queen problem using Backtracking. It mainly uses
  96.      * solveNQUtil() to solve the problem. It returns false if queens cannot be
  97.      * placed, otherwise return true and prints placement of queens in the form of
  98.      * 1s. Please note that there may be more than one solutions, this function
  99.      * prints one of the feasible solutions.
  100.      */
  101.     void solveNQ(int linha, int coluna) {
  102.         int board[][] = new int[N][N];
  103.  
  104.         if (solveNQUtil(board, 0, linha, coluna) == false) {
  105.             System.out.print("Solution does not exist");
  106.             return;
  107.         }
  108.  
  109.         return;
  110.     }
  111.  
  112.     // driver program to test above function
  113.     public static void main(String args[]) {
  114.         Scanner in = new Scanner(System.in);
  115.         int data_set = in.nextInt();
  116.         int linha = in.nextInt();
  117.         int coluna = in.nextInt();
  118.         NQueenProblem Queen = new NQueenProblem();
  119.         System.out.print("SOLN       COLUMN\n");
  120.         System.out.print(" #      1 2 3 4 5 6 7 8\n\n");
  121.         Queen.solveNQ(linha, coluna);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement