Yargi

FindTheRealQueen

Sep 22nd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FindTheRealQueen {
  4.     public static void main(String[] args) {
  5.  
  6.         int r = 8; int c = 8;
  7.         char[][] board = matrixReader(r, c);
  8.  
  9.         for (int i = 0; i < r; i++){
  10.             for (int j = 0; j < c; j++){
  11.                 if (board[i][j] == 'q'){
  12.                     if (checkNeighbours(i, j, board)) {
  13.                         System.out.println(i + " " + j);
  14.                     }
  15.                 }
  16.             }
  17.         }
  18.     }
  19.  
  20.     private static char[][] matrixReader(int r, int c){
  21.         char[][] matrix = new char[r][c];
  22.         Scanner sc = new Scanner(System.in);
  23.         for (int i = 0; i < r; i++){
  24.             String[] input = sc.nextLine().split("\\s+");
  25.             for (int j = 0; j < c; j++){
  26.                 matrix[i][j] = input[j].charAt(0);
  27.             }
  28.         }
  29.         sc.close();
  30.         return matrix;
  31.     }
  32.  
  33.     public static boolean checkNeighbours(int r, int c, char[][] board){
  34.  
  35.         // check north
  36.         for (int i = r - 1; i >= 0; i--){
  37.             if (board[i][c] == 'q')
  38.                 return false;
  39.         }
  40.         // check south
  41.         for(int i = r + 1; i < 8; i++){
  42.             if (board[i][c] == 'q')
  43.                 return false;
  44.         }
  45.         // check west
  46.         for(int i = c - 1; i >= 0; i--){
  47.             if (board[r][i] == 'q')
  48.                 return false;
  49.         }
  50.         // check east
  51.         for(int i = c + 1; i < 8; i++){
  52.             if (board[r][i] == 'q')
  53.                 return false;
  54.         }
  55.         // check northwest
  56.         int i = r;
  57.         int j = c;
  58.         while (i > 0 && j > 0){
  59.             i--;
  60.             j--;
  61.             if (board[i][j] == 'q')
  62.                 return false;
  63.         }
  64.         // check northeast
  65.         i = r;
  66.         j = c;
  67.         while (i > 0 && j < 7){
  68.             i--;
  69.             j++;
  70.             if (board[i][j] == 'q')
  71.                 return false;
  72.         }
  73.         // check southwest
  74.         i = r;
  75.         j = c;
  76.         while (i < 7 && j > 0){
  77.             i++;
  78.             j--;
  79.             if (board[i][j] == 'q')
  80.                 return false;
  81.         }
  82.         // check southeast
  83.         i = r;
  84.         j = c;
  85.         while (i < 7 && j < 7){
  86.             i++;
  87.             j++;
  88.             if (board[i][j] == 'q')
  89.                 return false;
  90.         }
  91.         return true;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment