Queen's Attack II Discussions | Algorithms | HackerRank

Sort by

recency

|

995 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        static int queensAttack(int n, int k, int r, int c, int[][] obstacles) {
            Set<String> obs = new HashSet<>();
            for (int[] ob : obstacles) obs.add(ob[0] + "," + ob[1]);
            int count = 0;
            int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
            for (int[] dir : dirs) {
                for (int i = 1; ; i++) {
                    int nr = r + dir[0] * i, nc = c + dir[1] * i;
                    if (nr < 1 || nr > n || nc < 1 || nc > n || obs.contains(nr + "," + nc)) break;
                    count++;
                }
            }
            return count;
        }
    
        public static void main(String[] args) throws IOException {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt(), k = sc.nextInt();
            int r = sc.nextInt(), c = sc.nextInt();
            int[][] obstacles = new int[k][2];
            for (int i = 0; i < k; i++) {
                obstacles[i][0] = sc.nextInt();
                obstacles[i][1] = sc.nextInt();
            }
            System.out.println(queensAttack(n, k, r, c, obstacles));
        }
    }
    
  • + 0 comments

    This is my Python Code. It works as expected:

    def queensAttack(n, k, r_q, c_q, obstacles):
    
        SQUARE_INF = 1
    
    
        queen_inital_pos = [r_q, c_q]
        # Updaters for movements for every queen's direction
        updaters = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
        
        max_movements_per_quadrant = {
            (-1, -1): min(r_q - SQUARE_INF, c_q - SQUARE_INF),
            (-1, 0): r_q - SQUARE_INF,
            (-1, 1): min(r_q - SQUARE_INF, n - c_q),
            (0, -1): c_q - SQUARE_INF,
            (0, 1):  n - c_q,
            (1, -1): min(n - r_q, c_q - SQUARE_INF),
            (1, 0): n - r_q,
            (1, 1): min(n - r_q, n - c_q),
        }
        # Faster than list for searching
        obstacles_set = {(r, c) for r, c in obstacles}
          
        all_movements = 0
    
        for updater in updaters:
           
            for step in range(1, max_movements_per_quadrant[updater] + 1):
                
                next_coord = (
                    queen_inital_pos[0] + (updater[0] * step), 
                    queen_inital_pos[1] + (updater[1] * step)
                )
                
                if next_coord in obstacles_set:
                    break
                
                all_movements += 1
            
        return all_movements
    
  • + 1 comment

    My Python code isn't really working. Can someone help me out?

    def queensAttack(n, k, r_q, c_q, obstacles):
        attacked = 0
        pos = [[r_q, c_q] for position in range(8)]
        moves = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
        moves.remove((0, 0))
        for move in range(len(moves)):
            while True:
                pos[move][0] += moves[move][0]
                pos[move][1] += moves[move][1]
                if not 1 <= pos[move][0] <= n:
                    break
                if not 1 <= pos[move][1] <= n:
                    break
                if (pos[move][0], pos[move][1]) in obstacles:
                    break
                attacked += 1
        return attacked
    
  • + 0 comments

    You are combining 2 numbers into single long digit. I wanted to know, how these tricks do people come to know? fapello

  • + 0 comments

    You are combining 2 numbers into single long digit. I wanted to know, how these tricks do people come to know?