Advertisement
emodev

Queen

Jan 19th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package f02_Matrices.Lab;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class P07_FindTheRealQueen {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         char[][] matrix = readMatrixFromConsole(reader);
  12.  
  13.         String position = "";
  14.         for (int i = 0; i < matrix.length; i++) {
  15.             for (int j = 0; j < matrix[0].length; j++) {
  16.                 char current = matrix[i][j];
  17.                 if (current == 'q') {
  18.                     if (!left(matrix, i, j) || !right(matrix, i, j) || !up(matrix, i, j) || !down(matrix, i, j)
  19.                             || !leftUp(matrix, i, j) || !leftDown(matrix, i, j) || !rightUp(matrix, i, j) || !rightDown(matrix, i, j)) {
  20.                         continue;
  21.                     }
  22.                     position = i + " " + j;
  23.                 }
  24.             }
  25.         }
  26.  
  27.         System.out.println(position);
  28.  
  29.     }
  30.  
  31.     private static char[][] readMatrixFromConsole(BufferedReader reader) throws IOException {
  32.  
  33.         int row = 8;
  34.         int col = 8;
  35.         char[][] matrix = new char[row][col];
  36.  
  37.         for (int i = 0; i < row; i++) {
  38.             String[] line = reader.readLine().split(" ");
  39.             for (int j = 0; j < col; j++) {
  40.                 matrix[i][j] = (line[j].charAt(0));
  41.             }
  42.         }
  43.         return matrix;
  44.     }
  45.  
  46.     private static boolean left(char[][] matrix, int row, int col) {
  47.         while (true) {
  48.  
  49.             col--;
  50.             if (col <= 0) {
  51.                 break;
  52.             }
  53.             char current = matrix[row][col];
  54.             if (current == 'q') {
  55.                 return false;
  56.             }
  57.         }
  58.         return true;
  59.     }
  60.  
  61.     private static boolean right(char[][] matrix, int row, int col) {
  62.         while (true) {
  63.             col++;
  64.             if (col > matrix[0].length - 1) {
  65.                 break;
  66.             }
  67.             char current = matrix[row][col];
  68.             if (current == 'q') {
  69.                 return false;
  70.             }
  71.         }
  72.         return true;
  73.     }
  74.  
  75.     private static boolean up(char[][] matrix, int row, int col) {
  76.         while (true) {
  77.             row--;
  78.             if (row < 0) {
  79.                 break;
  80.             }
  81.             char current = matrix[row][col];
  82.             if (current == 'q') {
  83.                 return false;
  84.             }
  85.         }
  86.         return true;
  87.     }
  88.  
  89.     private static boolean down(char[][] matrix, int row, int col) {
  90.         while (true) {
  91.             row++;
  92.             if (row > matrix.length - 1) {
  93.                 break;
  94.             }
  95.             char current = matrix[row][col];
  96.             if (current == 'q') {
  97.                 return false;
  98.             }
  99.         }
  100.         return true;
  101.     }
  102.  
  103.  
  104.     private static boolean leftUp(char[][] matrix, int row, int col) {
  105.         while (true) {
  106.             row--;
  107.             col--;
  108.             if (row < 0 || col < 0) {
  109.                 break;
  110.             }
  111.             char current = matrix[row][col];
  112.             if (current == 'q') {
  113.                 return false;
  114.             }
  115.         }
  116.         return true;
  117.     }
  118.  
  119.  
  120.     private static boolean leftDown(char[][] matrix, int row, int col) {
  121.         while (true) {
  122.             row++;
  123.             col--;
  124.             if (row > matrix.length - 1 || col < 0) {
  125.                 break;
  126.             }
  127.             char current = matrix[row][col];
  128.             if (current == 'q') {
  129.                 return false;
  130.             }
  131.         }
  132.         return true;
  133.     }
  134.  
  135.  
  136.     private static boolean rightUp(char[][] matrix, int row, int col) {
  137.         while (true) {
  138.             row--;
  139.             col++;
  140.             if (row < 0 || col > matrix[0].length - 1) {
  141.                 break;
  142.             }
  143.             char current = matrix[row][col];
  144.             if (current == 'q') {
  145.                 return false;
  146.             }
  147.         }
  148.         return true;
  149.     }
  150.  
  151.     private static boolean rightDown(char[][] matrix, int row, int col) {
  152.         while (true) {
  153.             row++;
  154.             col++;
  155.             if (row > matrix.length - 1 || col > matrix[0].length - 1) {
  156.                 break;
  157.             }
  158.             char current = matrix[row][col];
  159.             if (current == 'q') {
  160.                 return false;
  161.             }
  162.         }
  163.         return true;
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement