Queen's Attack II Discussions | Algorithms | HackerRank
  • + 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