-
백준 1604번 python 3 풀이백준 2020. 1. 24. 20:29반응형
from fractions import Fraction import sys class Line: def __init__(self, param): self.ax = int(param[0]) self.ay = int(param[1]) self.bx = int(param[2]) self.by = int(param[3]) # ax + by = c self.a = self.by - self.ay self.b = self.ax - self.bx self.c = self.a * self.ax + self.b * self.ay # def debug(self): # print([self.a, self.b, self.c]) def crossPoint(self, otherLine): de = self.a * otherLine.b - self.b * otherLine.a if de == 0: return False x = Fraction(self.c * otherLine.b - self.b * otherLine.c, de) y = Fraction(self.a * otherLine.c - self.c * otherLine.a, de) if (-10 < x and x < 10 and -10 < y and y < 10): return (x, y) else: return False if (x >= 10 or x <= -10): return False if (y >= 10 or y <= -10): return False print(x,y) return (x, y) def upperArea(self, point): return self.a * point[0] + self.b * point[1] - self.c >= 0 def downArea(self, point): return self.a * point[0] + self.b * point[1] - self.c <= 0 def inBox(self): # x = += 10, y += 10 같은 변태적인 경우 제외. # print(self.a, self.b, self.c) if self.a == 0: cb = Fraction(self.c) / Fraction(self.b) if cb >= 10 or cb <= -10: return False if self.b == 0: ca = Fraction(self.c) / Fraction(self.a) if ca >= 10 or ca <= -10: return False if (self.upperArea((-10, 10)) is True) and (self.upperArea((10, 10)) is True) and (self.upperArea((-10, -10)) is True) and (self.upperArea((10, -10)) is True): return False if (self.downArea((-10, 10)) is True) and (self.downArea((10, 10)) is True) and (self.downArea((-10, -10)) is True) and (self.downArea((10, -10)) is True): return False if (self.ax >= 10 and self.bx >= 10): return False if (self.ax <= -10 and self.bx <= -10): return False if (self.ay >= 10 and self.by >= 10): return False if (self.ay <= -10 and self.by <= -10): return False return True def main(): n = int(sys.stdin.readline()) lines = [] for i in range(0, n): a = sys.stdin.readline().split(' ') # print(a) line = Line(a) if line.inBox(): lines.append(line) answer = 1 for line in lines: answer += 1 # if line.inBox(): # print("in") # print(line.a,"x+", line.b,"y=", line.c) # print(line.ax, line.ay, line.bx, line.by) length = len(lines) for i in range(0, length): for j in range(i+1, length): line = lines[i] otherLine = lines[j] crossPoint = line.crossPoint(otherLine) if crossPoint != False: answer += 1 # else: # print("a") # print(pointSet) print(answer) main()
역대급 역겨운 문제였습니다.
반응형'백준' 카테고리의 다른 글
코드잼 2020 라운드 1B 후기 (0) 2020.04.20 백준 3015번 분할 정복식 풀이 (0) 2020.02.04 백준 1797번 C++ 풀이 (0) 2020.01.19 백준 13460번 C++ 코드 (0) 2020.01.15 백준 17370번 풀이 (0) 2019.12.03