Queen's Attack II Discussions | Algorithms | HackerRank
  • + 1 comment

    Here's my inefficient code :D I suck at grid problems

    def is_available_move(initial_position, i, j, obstacles, n):
        if (i > 0 and i <= n) and (j > 0 and j <= n):
            if [i,j] not in obstacles and (i, j) != initial_position:
                return True
        return False
    
    def queensAttack(n, k, r_q, c_q, obstacles):
        initial_position = (r_q, c_q) # queen's initial position
        
        # 8 directions from queen's initial position
        top_left_d = []
        top_v = []
        top_right_d = []
        right_h = []
        bottom_right_d = []
        bottom_v = []
        bottom_left_d = []
        left_h = []
        
        # find all moves from initial position to top left diagonal
        print("now calculating top_left_diagonal")
        j = c_q
        for i in range(r_q, n):
            i += 1
            j -= 1
            print("what's i",i, "what's j", j)
            if is_available_move(initial_position, i, j, obstacles, n):
                top_left_d.append((i,j))
            else:
                break
        print(top_left_d)
    
        # find all moves from initial position to top vertical 
        print("now calculating top_v")
        for i in range(r_q, n):
            i += 1 
            j = c_q
            if is_available_move(initial_position, i, j, obstacles, n):
                top_v.append((i, j))
            else:
                break
        print(top_v)
                
        # find all moves from initial position to top right diagonal 
        print("now calculating top_right_diagonal")
        j = c_q
        for i in range(r_q, n):
            i += 1  
            j += 1
            if is_available_move(initial_position, i, j, obstacles, n):
                top_right_d.append((i,j))
            else:
                break
        print(top_right_d)
                    
        # find all moves from initial position to right horizontal row
        print("now calculating right_h")
        for j in range(c_q, n):
            j += 1 
            i = r_q
            if is_available_move(initial_position, i, j, obstacles, n):
                right_h.append((i,j))
            else:
                break
        print(right_h) 
        
        # find all moves from initial position to bottom right diagonal
        print("now calculating bottom_right_diagonal")
        j = c_q
        for i in range(r_q , 0, -1):
            i -= 1
            j += 1
            if is_available_move(initial_position, i, j, obstacles, n):
                bottom_right_d.append((i,j))
            else:
                break
        print(bottom_left_d)
                    
                    
        # find all moves from initial position to bottom vertical 
        print("now calculating bottom_v")
        for i in range(r_q, 0, -1):
            i -= 1
            j = c_q
            if is_available_move(initial_position, i, j, obstacles, n):
                bottom_v.append((i, j))
            else:
                break
        print(bottom_v)
        
        # find all moves from initial position to bottom left diagonal
        print("now calculating bottom_left_diagonal")
        j = c_q
        for i in range(r_q, 0, -1):
            i -= 1
            j -= 1
            if is_available_move(initial_position, i, j, obstacles, n):
                bottom_left_d.append((i,j))
            else:
                break
        print(bottom_left_d)
                
        # find all moves from initial position to left horizontal row
        print("now calculating left_h")
        for j in range(c_q, 0, -1):
            j -= 1
            i = r_q
            if is_available_move(initial_position, i, j, obstacles, n):
                left_h.append((i,j))        
            else:
                break
        print(left_h)
    
        # print("what's our moves: ", top_left_d + top_v + top_right_d + right_h + bottom_right_d + bottom_v + bottom_left_d + left_h)
        return len(top_left_d + top_v + top_right_d + right_h + bottom_right_d + bottom_v + bottom_left_d + left_h)