• + 0 comments

    good problem, only took me 4 hours trying to define the collision equation before i shoot myself.

    def twoPluses(grid):
        n = len(grid)
        m = len(grid[0])
        
        def get_pluses(i, j):
            s = 1
            res = [[[i, i], [j, j]]]
            delta_table = [(0, s), (0, -s), (s, 0), (-s, 0)]
            stop_flag = False
            while not stop_flag:
                for d in delta_table:
                    if not (i + d[0] >= 0 and i + d[0] <= n-1 and j + d[1] >= 0 and j + d[1] <= m-1 and grid[i+d[0]][j+d[1]] == 'G'):
                        stop_flag = True
                if not stop_flag:
                    res.append([[j-s, j+s], [i-s, i+s]])
                    s +=1
                    delta_table = [(0, s), (0, -s), (s, 0), (-s, 0)]
            
            return res
                    
                
        # Write your code here
        pluses_l = []
        for i in range(n):
            for j in range(m):
                plus = get_pluses(i, j)
                if plus:
                    pluses_l += plus
    
        #plus: [[x1,x2], [y1, y2]]
        mx = 0
        for i in range(len(pluses_l)):
            compatible_l = []
            for j in range(len(pluses_l)):
                x1, x2, xp_1, xp_2 = pluses_l[i][0][0], pluses_l[i][0][1], pluses_l[j][0][0], pluses_l[j][0][1]
                y1, y2, yp_1, yp_2 = pluses_l[i][1][0], pluses_l[i][1][1], pluses_l[j][1][0], pluses_l[j][1][1]
                
                x_m, xp_m = (x1 + x2)/2, (xp_1 + xp_2)/2
                y_m, yp_m = (y1 + y2)/2, (yp_1 + yp_2)/2
                is_y_collision = (xp_1 <= x_m and xp_2 >= x_m and (yp_m >= y1 and yp_m <= y2))
                is_x_collision = (yp_1 <= y_m and yp_2 >= y_m and (xp_m >= x1 and xp_m <= x2))
                is_y_y_collision = xp_m == x_m and (min(y2, yp_2) >= max(y1, yp_1))
                is_x_x_collision = yp_m == y_m and (min(x2, xp_2) >= max(x1, xp_1))
                if not (is_x_collision or is_y_collision or is_x_x_collision or is_y_y_collision):
                    compatible_l.append(pluses_l[j])
            
            a1 = (pluses_l[i][0][1] - pluses_l[i][0][0]+1) * 2 - 1
            if not compatible_l:
                mx = max(mx, a1)
            for p_c in compatible_l:
                a2 = (p_c[0][1] - p_c[0][0]+1) * 2 - 1
                mx = max(mx, a1 * a2)
                
        return mx