Queen's Attack II Discussions | Algorithms | HackerRank

Sort by

recency

|

982 Discussions

|

  • + 0 comments

    My Python solution: ' def queensAttack(n, k, r_q, c_q, obstacles): directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)] total = 0 # obstacles = set(obstacles) obstacles = set(tuple(obs) for obs in obstacles) for dx, dy in directions: x, y = r_q, c_q while True: x += dx y += dy if not (1 <= x <= n): break if not (1 <= y <= n): break if (x, y) in obstacles: break total += 1 return total

    '

  • + 0 comments

    If you change the part of the code that reads the problem input to make a list of tuples as opposed to a list of lists, this will work on all test cases:

    def queensAttack(n, k, r_q, c_q, obstacles):
        directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        total = 0
        obstacles = set(obstacles)
        for dx, dy in directions:
            x, y = r_q, c_q
            while True:
                x += dx
                y += dy
                if not (1 <= x <= n):
                    break
                if not (1 <= y <= n):
                    break
                if (x, y) in obstacles:
                    break
                total += 1  
        return total
    
  • + 0 comments

    EASIER JAVA SOLUTION;

    import java.util.*;
    
    public final class Solution{
        public static void main(String[] args){
            int arraySize, qRow, qCol;
            
             HashSet<List<Integer>> s = new HashSet<>();
             try (Scanner in = new Scanner(System.in)) {
            
            arraySize = in.nextInt();
            int numberObstacles = in.nextInt();
             qRow= in.nextInt();
             qCol= in.nextInt();
             while(numberObstacles-->0){
                 int obsRow = in.nextInt();
                 int obsCol = in.nextInt();
                 s.add(Arrays.asList(obsRow, obsCol));
             }
             }
             
             int position[][] = new int[][]{{0,1}, {1,0}, {-1,0},{0,-1}, {1,1},{-1,1},{1,-1},{-1,-1}}; 
             long ans=0;
             for(int i=0; i<position.length;i++){
                 int row = qRow+position[i][0];
                 int col = qCol+position[i][1];
                 while(!s.contains(Arrays.asList(row, col))){
                    //  System.out.println(s.contains(Arrays.asList(row, col)));
                    //  System.out.printf("using:(%d,%d)==> %d, %d\n", position[i][0], position[i][0], row, col);
                     if( row <1 || row > arraySize){
                         break;
                     }
                     if( col <1 || col > arraySize){
                         break;
                     }
                     ans+=1;
                     
                     
                     row+=position[i][0];
                     col+=position[i][1];
                 }
                 
             }
                 System.out.println(ans);
    }
             
             
            COMMENT FOR PYTHON SOLUTION AS WELL
            
            
        }
    } 
    
  • + 0 comments

    def queensAttack(n, k, r_q, c_q, obstacles): # Create a set of obstacle positions obstacles = set(tuple(obstacle) for obstacle in obstacles)

    # Directions for the queen's movements
    directions = [
        (1, 0),   # Down
        (-1, 0),  # Up
        (0, 1),   # Right
        (0, -1),  # Left
        (1, 1),   # Down-Right
        (1, -1),  # Down-Left
        (-1, 1),  # Up-Right
        (-1, -1)  # Up-Left
    ]
    
    total = 0
    
    for dx, dy in directions:
        x, y = r_q, c_q
    
        # Initialize the maximum reachable distance in this direction
        max_distance = n  # maximum distance to the edge of the board
    
        while True:
            x += dx
            y += dy
    
            # Check if the new position is within the board limits
            if not (1 <= x <= n and 1 <= y <= n):
                break
    
            # Check if there's an obstacle
            if (x, y) in obstacles:
                break
    
            total += 1  # Increment the count of positions the queen can attack
    
    return total
    
  • + 1 comment
    import java.util.*;
    
    public final class Solution {
      public static final void main(String[] args) {
        int n, rq, cq;
        Set<Long> o;
        try (Scanner in = new Scanner(System.in)) {
          n = in.nextInt();
          int k = in.nextInt();
          rq = in.nextInt();
          cq = in.nextInt();
          o = new HashSet<>(k);
          while (k --> 0) {
            int ro = in.nextInt(), co = in.nextInt();
            o.add((long)ro << 32 | co);
          }
        }
        int t = 0;
        for (int d[] : new int[][] {{-1, -1}, {-1,  0}, {-1, +1},
                                    { 0, -1},           { 0, +1},
                                    {+1, -1}, {+1,  0}, {+1, +1}}) {
          for (int r = rq + d[0], c = cq + d[1];
               1 <= r && r <= n && 1 <= c && c <= n && !o.contains((long)r << 32 | c);
               r += d[0], c += d[1]) {
            t++;
          }
        }
        System.out.println(t);
      }
    }