eulaliaaires

8Queens

May 26th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     static class QueenProblem {
  6.         final int N = 8;
  7.         int k = 1;
  8.         int counter = 1;
  9.  
  10.         void printSolution(int board[][], int linha, int coluna) {
  11.             if (board[linha - 1][coluna - 1] == 1) {
  12.                 if (k <= 9) {
  13.                     System.out.print(" " + k++ + "      ");
  14.                 } else {
  15.                     System.out.print(k++ + "      ");
  16.                 }
  17.                 for (int j = 0; j < N; j++) {
  18.                     for (int i = 0; i < N; i++) {
  19.                         if (board[i][j] == 1) {
  20.                             if (counter <= 7) {
  21.                                 System.out.print((i + 1) + " ");
  22.                                 counter++;
  23.                                 break;
  24.                             } else {
  25.                                 System.out.print(i + 1);
  26.                                 counter++;
  27.                                 break;
  28.                             }
  29.                         }
  30.                     }
  31.                 }
  32.                 System.out.println();
  33.             }
  34.             counter = 0;
  35.         }
  36.  
  37.         boolean isSafe(int board[][], int row, int col) {
  38.             int i, j;
  39.  
  40.             /* Check this row on left side */
  41.             for (i = 0; i < col; i++)
  42.                 if (board[row][i] == 1)
  43.                     return false;
  44.  
  45.             /* Check upper diagonal on left side */
  46.             for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
  47.                 if (board[i][j] == 1)
  48.                     return false;
  49.  
  50.             /* Check lower diagonal on left side */
  51.             for (i = row, j = col; j >= 0 && i < N; i++, j--)
  52.                 if (board[i][j] == 1)
  53.                     return false;
  54.  
  55.             return true;
  56.         }
  57.  
  58.         boolean solveNQUtil(int board[][], int col, int linha, int coluna) {
  59.  
  60.             if (col >= N) {
  61.                 printSolution(board, linha, coluna);
  62.                 return true;
  63.             }
  64.  
  65.             boolean res = false;
  66.             for (int i = 0; i < N; i++) {
  67.  
  68.                 if (isSafe(board, i, col)) {
  69.                     /* Place this queen in board[i][col] */
  70.                     board[i][col] = 1;
  71.  
  72.                     res = solveNQUtil(board, col + 1, linha, coluna) || res;
  73.  
  74.                     board[i][col] = 0; // BACKTRACK
  75.                 }
  76.             }
  77.             return res;
  78.         }
  79.  
  80.         void solveNQ(int linha, int coluna) {
  81.             int board[][] = new int[N][N];
  82.  
  83.             if (solveNQUtil(board, 0, linha, coluna) == false) {
  84.                 System.out.print("Solution does not exist");
  85.                 return;
  86.             }
  87.             return;
  88.         }
  89.  
  90.         // driver program to test above function
  91.         public static void main(String args[]) {
  92.             Scanner in = new Scanner(System.in);
  93.             int data_set = in.nextInt();
  94.             for (int a = 0; a < data_set; a++) {
  95.                 int linha = in.nextInt();
  96.                 int coluna = in.nextInt();
  97.                 QueenProblem Queen = new QueenProblem();
  98.                 System.out.print("SOLN       COLUMN\n");
  99.                 System.out.print(" #      1 2 3 4 5 6 7 8\n\n");
  100.                 Queen.solveNQ(linha, coluna);
  101.                 if (a != data_set - 1) {
  102.                     System.out.println();
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment