Advertisement
eulaliaaires

8Queens (Presentation Error)

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