We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Queen's Attack II
Queen's Attack II
Sort by
recency
|
1000 Discussions
|
Please Login in order to post a comment
int queensAttack(int n, int k, int r_q, int c_q, vector> obstacles) {
}
Hint 1- Just check all obstacles that they are on one of four lines. Then you will have at maximun eight obstacles. Hint 2 - then just calculate distance to obstacle or edge of the board.
public class Solution {
// Complete the queensAttack function below. static int queensAttack(int n, int k, int r, int c, int[][] obstacles) { HashMap> cache = new HashMap<>(); for (int i = 0; i < obstacles.length; i++) { if (cache.containsKey(obstacles[i][0])) { cache.get(obstacles[i][0]).add(obstacles[i][1]); } else { cache.put(obstacles[i][0], new HashSet()); cache.get(obstacles[i][0]).add(obstacles[i][1]); } } int counter = 0; // right for (int i = c + 1; i <= n; i++) { if (cache.containsKey(r) && cache.get(r).contains(i)) { break; } counter++; }
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
} }
Python 3 My code runs by doing a few things for each calculation: 1. determine all the obstacles that are in line with my queen (above and below, left and right, top left to bottom right diagonal, and top right to bottom left diagonal) 2. to count an attackable square it needs to be: in bounds and before the first object in line with the queen in the given direction 3. for the 4 cardinal directions (up, down, left, right) I only need to check the appriate row or column, for the 4 ordinal directions (the diagonals) I must check for the appropriate row and column 4. I finally add the available attacks from all 8 directions and return it
dawg