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
|
982 Discussions
|
Please Login in order to post a comment
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
'
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:
EASIER JAVA SOLUTION;
def queensAttack(n, k, r_q, c_q, obstacles): # Create a set of obstacle positions obstacles = set(tuple(obstacle) for obstacle in obstacles)