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
|
1012 Discussions
|
Please Login in order to post a comment
import java.util.*; import java.awt.Point;
//thank me later public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in);
}
Cries in no. 1 grid problem hater:
step 1: calculate the maximum attacks in eight directions; step 2: if there is obstacle in any one of the eight direction, which one smaller is the distance. def queensAttack(n, k, r_q, c_q, obstacles): # Write your code here dir_dis = {(0,1):n-c_q, (0,-1):c_q - 1, (1, 0): n-r_q, (-1, 0): r_q - 1, (-1, -1):min(r_q - 1, c_q - 1), (1, 1):min(n-r_q, n-c_q), (1, -1): min(n-r_q, c_q-1), (-1, 1):min(r_q-1, n-c_q) } for ro, co in obstacles: dx = ro - r_q dy = co - c_q if dx == 0 or dy == 0 or abs(dx) == abs(dy): dis = abs(dx) if abs(dx) == abs(dy) else max(abs(dx), abs(dy)) di = (dx/dis, dy/dis) dir_dis[di] = min(dir_dis[di], dis - 1) return sum(dir_dis.values())
This code works..!! Huhh.. finallyy..!!:)) def queensAttack(n, k, r_q, c_q, obstacles): obstacle_set = {(r, c) for r, c in obstacles} moves = 0 directions = [ (1, 0), # Up (-1, 0), # Down (0, 1), # Right (0, -1), # Left (1, 1), # Up-right (1, -1), # Up-left (-1, 1), # Down-right (-1, -1) # Down-left ] for dr, dc in directions: r, c = r_q + dr, c_q + dc while 1 <= r <= n and 1 <= c <= n: if (r, c) in obstacle_set: break moves += 1 r += dr c += dc return moves if name == 'main': n, k = map(int, input().split()) r_q, c_q = map(int, input().split()) obstacles = [tuple(map(int, input().split())) for _ in range(k)] print(queensAttack(n, k, r_q, c_q, obstacles))
`
Here's my inefficient code :D I suck at grid problems