Advertisement
K_S_

Untitled

Nov 15th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. // Runtime: 1 ms, faster than 98.97% of Java online submissions for Queens That Can Attack the King.
  2. // Memory Usage: 36.5 MB, less than 100.00% of Java online submissions for Queens That Can Attack the King.
  3.  
  4.  
  5. class Solution {
  6.     public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
  7.         boolean[][] fields = new boolean[8][8];
  8.         for (int[] queen : queens) {
  9.             fields[queen[0]][queen[1]] = true;
  10.         }
  11.  
  12.         List<List<Integer>> result = new ArrayList<>();
  13.         for (int[] queen : queens) {
  14.             if (canAttack(queen, king, fields)) {
  15.                 List<Integer> q = new ArrayList<>();
  16.                 q.add(queen[0]);
  17.                 q.add(queen[1]);
  18.                 result.add(q);
  19.             }
  20.         }
  21.         return result;
  22.     }
  23.  
  24.     boolean canAttack(int[] queen, int[] king, boolean[][] fields) {
  25.         int x = queen[0];
  26.         int y = queen[1];
  27.         int k = king[0];
  28.         int l = king[1];
  29.  
  30.         if (x == k) {
  31.             if (y < l) {
  32.                 for (int i = y + 1; i < l; i++) {
  33.                     if (fields[x][i]) {
  34.                         return false;
  35.                     }
  36.                 }
  37.             } else {
  38.                 for (int i = y - 1; i > l; i--) {
  39.                     if (fields[x][i]) {
  40.                         return false;
  41.                     }
  42.                 }
  43.             }
  44.             return true;
  45.         }
  46.         if (y == l) {
  47.             if (x < k) {
  48.                 for (int i = x + 1; i < k; i++) {
  49.                     if (fields[i][y]) {
  50.                         return false;
  51.                     }
  52.                 }
  53.             } else {
  54.                 for (int i = x - 1; i > k; i--) {
  55.                     if (fields[i][y]) {
  56.                         return false;
  57.                     }
  58.                 }
  59.             }
  60.             return true;
  61.         }
  62.  
  63.         if (Math.abs(x - k) == Math.abs(y - l)) {
  64.             int j = 1;
  65.             if ((x - k) < 0) {
  66.                 if ((y - l) >= 0) {
  67.                     j *= -1;
  68.                 }
  69.                 for (int i = 1; i < k - x; i++) {
  70.                     if (fields[x + i][y + i*j]) {
  71.                         return false;
  72.                     }
  73.                 }
  74.  
  75.             } else {
  76.                 if ((y - l) >= 0) {
  77.                     j *= -1;
  78.                 }
  79.                 for (int i = 1; i < x - k; i++) {
  80.                     if (fields[x - i][y + i*j]) {
  81.                         return false;
  82.                     }
  83.                 }
  84.  
  85.             }
  86.             return true;
  87.         }
  88.  
  89.         return false;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement