Advertisement
SotirovG

FindTheRealQueen_07/JavaAdvanced

Sep 19th, 2021
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package JavaProModule.JavaAdvanced.MultidimensionalArraysLab;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FindTheRealQueen_07 {
  6.     public static void main(String[] args) {
  7.         Scanner read = new Scanner(System.in); trimReader(read);
  8.  
  9.         int rows = 8;
  10.         int cols = 8;
  11.         char[][] board = new char[rows][cols];
  12.  
  13.  
  14.  
  15.         for (int row = 0; row < board.length; row++) {
  16.             for (int col = 0; col < board[row].length; col++) {
  17.                 if (board[row][col] == 'q' && isValidQ(board, row, col)) {
  18.                     System.out.println(row + " " + col);
  19.                 }
  20.             }
  21.         }
  22.  
  23.  
  24.     }
  25.  
  26.     private static String trimReader(Scanner read) {
  27.         String input = read.nextLine().trim();
  28.         while(input.length() == 0){
  29.             input = read.nextLine().trim();
  30.         }
  31.         return input;
  32.     }
  33.  
  34.     private static char[][] readMatrix(Scanner read, int cols, int rows) {
  35.        char [][] myMatrix = new char[rows][cols];
  36.  
  37.         for (int row = 0; row < rows; row++) {
  38.            String input = trimReader(read);
  39.             String line = input.replaceAll("\\s+","");
  40.             myMatrix[row] = line.toCharArray();
  41.         }
  42.  
  43.  
  44.  
  45.         return myMatrix;
  46.     }
  47.  
  48.     private static boolean isValidQ(char[][] board, int row, int col) {
  49.         for (int rowDirect = -1; rowDirect <= 1; rowDirect++) {
  50.             for (int colDirect = -1; colDirect <= 1; colDirect++) {
  51.  
  52.                 if (rowDirect == 0 && colDirect == 0){
  53.                     continue;
  54.                 }
  55.  
  56.                 int currentRow = row + rowDirect;
  57.                 int currentCol = col + colDirect;
  58.  
  59.                 boolean inBounds = isInBounds(board, currentRow, currentCol);
  60.  
  61.                 while (inBounds){
  62.                     if (board[currentRow][currentCol] == 'q'){
  63.                         return false;
  64.                     }
  65.                     currentRow = currentRow + rowDirect;
  66.                     currentCol = currentCol + colDirect;
  67.                     inBounds = isInBounds(board,currentRow,currentCol);
  68.                 }
  69.             }
  70.         }
  71.         return false;
  72.     }
  73.  
  74.     private static boolean isInBounds(char[][] board, int currentRow, int currentCol) {
  75.         return currentRow >= 0 && currentRow < board.length && currentCol >= 0
  76.                 && currentCol < board[currentRow].length;
  77.     }
  78.  
  79.  
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement