Advertisement
Ligh7_of_H3av3n

07. Find The Real Queen

May 16th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package Lekciq;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FindTheRealQueen {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         // Read the chessboard matrix
  11.         char[][] chessboard = readChessboard(scanner);
  12.  
  13.         // Find the position of the valid queen
  14.         int[] validQueenPosition = findValidQueenPosition(chessboard);
  15.  
  16.         // Print the position of the valid queen
  17.         System.out.println(validQueenPosition[0] + " " + validQueenPosition[1]);
  18.     }
  19.  
  20.     // Method to read the chessboard matrix from the console
  21.     private static char[][] readChessboard(Scanner scanner) {
  22.         char[][] chessboard = new char[8][8];
  23.         for (int i = 0; i < 8; i++) {
  24.             String line = scanner.nextLine();
  25.             for (int j = 0; j < 8; j++) {
  26.                 chessboard[i][j] = line.charAt(j * 2);
  27.             }
  28.         }
  29.         return chessboard;
  30.     }
  31.  
  32.     // Method to find the position of the valid queen
  33.     private static int[] findValidQueenPosition(char[][] chessboard) {
  34.         int[] validQueenPosition = new int[2];
  35.  
  36.         // Iterate over each cell in the chessboard
  37.         for (int i = 0; i < 8; i++) {
  38.             for (int j = 0; j < 8; j++) {
  39.                 if (chessboard[i][j] == 'q') {
  40.                     // Check if the current position is valid
  41.                     if (isValidQueenPosition(chessboard, i, j)) {
  42.                         validQueenPosition[0] = i;
  43.                         validQueenPosition[1] = j;
  44.                         return validQueenPosition;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         return validQueenPosition;
  50.     }
  51.  
  52.     // Method to check if the queen at the given position is valid
  53.     private static boolean isValidQueenPosition(char[][] chessboard, int row, int col) {
  54.         // Check horizontally and vertically
  55.         for (int i = 0; i < 8; i++) {
  56.             if (chessboard[row][i] == 'q' && i != col) return false;
  57.             if (chessboard[i][col] == 'q' && i != row) return false;
  58.         }
  59.  
  60.         // Check diagonally (top-left to bottom-right)
  61.         for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
  62.             if (chessboard[i][j] == 'q') return false;
  63.         }
  64.         for (int i = row + 1, j = col + 1; i < 8 && j < 8; i++, j++) {
  65.             if (chessboard[i][j] == 'q') return false;
  66.         }
  67.  
  68.         // Check diagonally (top-right to bottom-left)
  69.         for (int i = row - 1, j = col + 1; i >= 0 && j < 8; i--, j++) {
  70.             if (chessboard[i][j] == 'q') return false;
  71.         }
  72.         for (int i = row + 1, j = col - 1; i < 8 && j >= 0; i++, j--) {
  73.             if (chessboard[i][j] == 'q') return false;
  74.         }
  75.  
  76.         return true;
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement