목차

교점에 별 만들기

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호87377
문제명점에 별 만들기
레벨Level 2
분류

구현

시간복잡도O(n^2 + x*y)
인풋사이즈n<=1000, x<=1000, y<=1000
사용한 언어Python
해결날짜2021/10/21

풀이

코드

"""Solution code for "Programmers 87377. 교점에 별 만들기".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/87377
- Solution link: http://www.teferi.net/ps/problems/programmers/87377
"""

import itertools


def solution(line):
    intersect_x_points, intersect_y_points = [], []
    for (a, b, e), (c, d, f) in itertools.combinations(line, 2):
        if (denom := a * d - b * c) == 0:
            continue
        x, r1 = divmod(b * f - e * d, denom)
        y, r2 = divmod(e * c - a * f, denom)
        if r1 == r2 == 0:
            intersect_x_points.append(x)
            intersect_y_points.append(y)
    min_x, max_x = min(intersect_x_points), max(intersect_x_points)
    min_y, max_y = min(intersect_y_points), max(intersect_y_points)
    grid = [['.'] * (min_x - max_x + 1) for _ in range(min_y - max_y + 1)]
    for x, y in zip(intersect_x_points, intersect_y_points):
        grid[y - min_y][x - min_x] = '*'
    return [''.join(row) for row in grid]