practivceAlgorithm 570

[백준][Python] 13913 숨바꼭질4

경로에 대한 조사는 둘중하나다. 같이 들고 다니던가. 아니면 pre_node 배열을 만들던가. import sys input = sys.stdin.readline from collections import deque def bfs(start,target): queue = deque() queue.append([start,0,[start]]) visited = [False]*400001 visited[start] = True while queue: cur,cur_time,cur_path = queue.popleft() if cur==target: return cur_time ,cur_path for next_node in (cur-1,cur+1,cur*2): if 0

[백준][Python] 2581 소수

에라토스테네스의 seive 써서 구했다. 모든 숫자를 최댓 범위의 제곱근 범위까지 돌며 그 배수를 False로 바꿔주는게 포인트. i는 False로 바꾸지 않는다(소수이기 때문) i+i부터 바꾼다. import sys input = sys.stdin.readline def che(): nums = [True]*(N+1) nums[0]=nums[1]=False primes = [] for i in range(2,int(N**0.5)+1): if nums[i]: for j in range(i+i, N+1, i): nums[j] = False return [i for i in range(M,N+1) if nums[i]] M = int(input()) N = int(input()) arr = che() if a..

[백준][Python] 14696 딱지놀이

카드 숫자 센다음 큰 숫자부터 갯수 비교 import sys input = sys.stdin.readline N = int(input()) for _ in range(N): a_cnt = [0]*5 b_cnt = [0]*5 a_n, *a_card = map(int, input().split()) b_n, *b_card = map(int, input().split()) for card in a_card: a_cnt[card] += 1 for card in b_card: b_cnt[card] += 1 for i in range(1,5): if a_cnt[-i] > b_cnt[-i]: print('A') break elif a_cnt[-i] < b_cnt[-i]: print('B') break else: pr..

[백준][Python] 10163 색종이

색종이 덧칠하고 순회하며 갯수 세줬습니다. import sys input = sys.stdin.readline N = int(input()) matrix = [[0]*1001 for _ in range(1001)] for k in range(1,N+1): x,y,w,h = map(int, input().split()) for i in range(x,x+w): for j in range(y,y+h): matrix[i][j] = k cnt_color = [0] * (N+1) for i in range(1001): for j in range(1001): if matrix[i][j]: cnt_color[matrix[i][j]] += 1 for i in range(1,N+1): print(cnt_color[i])

[백준][Python] 10158 개미

좌표평면에서 기준선 0, w,h 축을 기준으로 범위 안에 들어올때까지 대칭시키는 방법. import sys input = sys.stdin.readline w, h = map(int, input().split()) p, q = map(int, input().split()) t = int(input()) init_x = p + t init_y = q + t if not (init_x//w)&1: x = (-2*w*((init_x//w)//2) + init_x) else: x = 2*w - (-2*w*((init_x//w)//2) + init_x) if not (init_y//h)&1: y = (-2*h*((init_y//h)//2) + init_y) else: y = 2*h - (-2*h*((init_y/..