Guest User

Untitled

a guest
Mar 22nd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //You are given coordinates of two queens on a chess board. Find out, whether they hit each other or not.
  2. //
  3. // INPUT
  4. // Four integer numbers x1,y1,x2,y2 are being typed.
  5. //
  6. // OUTPUT
  7. // Type "YES" (uppercase) if they hit each other or "NO" if the don't.
  8.  
  9.  
  10. package OlksSereda.TextParser;
  11.  
  12. import java.util.Scanner;
  13.  
  14. public class Main {
  15.  
  16. public static void main(String[] args) {
  17. Scanner sc = new Scanner(System.in);
  18. int[] i = new int[4];
  19. for (int j = 0; j < 4; j++) {
  20. i[j] = sc.nextInt();
  21. }
  22. boolean b = hit(i);
  23. if (b) System.out.println("YES");
  24. else System.out.println("NO");
  25.  
  26. }
  27.  
  28. public static boolean hit(int[] i) {
  29.  
  30. int[][] matrix = new int[8][8];
  31. for (int j = 0; j < 8; j++) {
  32. for (int k = 0; k < 8; k++) {
  33. matrix[j][k] = 0;
  34. }
  35. }
  36. matrix[i[0]][i[1]] = 1; //set qeen1
  37. matrix[i[2]][i[3]] = 1; //set qeen2
  38. matrix = line(i[0], i[1], matrix); //calqulate line attack
  39. matrix = diagonal(i[0], i[1], matrix); //calculate diagonal attack
  40. PrintMatrix(matrix); //print martix
  41. return resultF(matrix);
  42. }
  43.  
  44. private static void PrintMatrix(int[][] matrix) {
  45. for (int j = 0; j < 8; j++) {
  46. for (int k = 0; k < 8; k++) {
  47. System.out.print(matrix[j][k] + " ");
  48. }
  49. System.out.println();
  50. }
  51. }
  52.  
  53. private static boolean resultF(int[][] matrix) {
  54. boolean res = false;
  55. for (int j = 0; j < 8; j++) {
  56. for (int k = 0; k < 8; k++) {
  57. if (matrix[k][j] > 1) res = true;
  58. }
  59. }
  60. return res;
  61.  
  62. }
  63.  
  64. private static int[][] line(int x, int y, int[][] matrix) {
  65. for (int j = 0; j < 8; j++) {
  66. matrix[x][j]++;
  67. }
  68. for (int j = 0; j < 8; j++) {
  69. matrix[j][y]++;
  70. }
  71. matrix[x][y] = matrix[x][y] - 2;
  72. return matrix;
  73. }
  74.  
  75. private static int[][] diagonal(int x, int y, int[][] matrix) {
  76. int[] coordinates = {x, y};
  77. while ((coordinates[0] < 7) & (coordinates[1] < 7)) {
  78. coordinates[0]++;
  79. coordinates[1]++;
  80. matrix[coordinates[0]][coordinates[1]]++;
  81. }
  82. coordinates[0] = x;
  83. coordinates[1] = y;
  84. while ((coordinates[0] > 0) & (coordinates[1] > 0)) {
  85. coordinates[0]--;
  86. coordinates[1]--;
  87. matrix[coordinates[0]][coordinates[1]]++;
  88. }
  89. coordinates[0] = x;
  90. coordinates[1] = y;
  91. while ((coordinates[0] < 7) & (coordinates[1] > 0)) {
  92. coordinates[0]++;
  93. coordinates[1]--;
  94. matrix[coordinates[0]][coordinates[1]]++;
  95. }
  96. coordinates[0] = x;
  97. coordinates[1] = y;
  98. while ((coordinates[0] > 0) & (coordinates[1] < 7)) {
  99. coordinates[0]--;
  100. coordinates[1]++;
  101. matrix[coordinates[0]][coordinates[1]]++;
  102. }
  103.  
  104. return matrix;
  105. }
  106. }
Add Comment
Please, Sign In to add comment