Advertisement
ogv

Untitled

ogv
Nov 15th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 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.3 MB, less than 100.00% of Java online submissions for Queens That Can Attack the King.
  3. class Solution {    
  4.     public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
  5.         char[][] board = new char[8][8];                
  6.         for (int[] queen: queens) board[queen[0]][queen[1]] = 'Q';
  7.        
  8.         List<List<Integer>> result = new ArrayList<>();
  9.  
  10.         int[][] directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1} };
  11.        
  12.         for (int[] direction: directions) {
  13.             int r = king[0] + direction[0];
  14.             int c = king[1] + direction[1];
  15.             while (r >= 0 && r < 8 && c >=0 && c < 8) {
  16.                 if (board[r][c] == 'Q') {
  17.                     result.add(Arrays.asList(r, c));
  18.                     break;
  19.                 }
  20.                
  21.                 r += direction[0];
  22.                 c += direction[1];
  23.             }
  24.         }
  25.        
  26.         return result;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement