본문 바로가기

전체 글92

[python] 17471. 게리맨더링 문제 출처 17471. 게리맨더링 풀이 from itertools import combinations N = int(input()) population = list(map(int, input().split())) connection = {i: [j-1 for j in list(map(int, input().split()))[1:]] for i in range(N)} def is_connected(comb): check = [1 if i in comb else 0 for i in range(N)] visited = [0]*N visited[list(comb)[0]] = 1 q = connection[list(comb)[0]] while q: nxt = [] for i in q: if check[i] and.. 2019. 10. 19.
[python] 17143. 낚시왕 문제 출처 17143. 낚시왕 풀이 from collections import deque R, C, M = map(int, input().split()) sharks = [list(map(int, input().split())) for _ in range(M)] # y, x, 속력, 방향, 크기 dr, dc = (0, -1, 1, 0, 0), (0, 0, 0, 1, -1) def move(shark): r, c, s, d, z = shark while s > 0: nr, nc = r + dr[d], c + dc[d] s -= 1 if nr > R: d = 1 nr -= 2 elif nr C: d = 4 nc -= 2 elif nc < 1: d = 3 n.. 2019. 10. 19.
[python] 17472. 다리 만들기 2 문제 출처 17472. 다리 만들기 2 풀이 from collections import deque N, M = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(N)] visited = [[0]*M for _ in range(N)] dx, dy = (1, 0, -1, 0), (0, 1, 0, -1) def numbering_irlands(x, y): visited[y][x] = 1 arr[y][x] = irland q = deque() q.append((x, y)) while q: x, y = q.popleft() for i in range(4): nx, ny = x + dx[i], y + dy[i] if nx .. 2019. 10. 19.
[python] 2146. 다리 만들기 문제 출처 2146. 다리 만들기 풀이 from collections import deque # 섬에 넘버링 def numbering_irlands(x, y): arr[y][x] = irland q = deque() q.append((x, y)) while q: x, y = q.popleft() visited[y][x] = 1 for i in range(4): nx, ny = x + dx[i], y + dy[i] if nx = N or ny = N: continue if not visited[ny][nx] and arr[ny][nx]: arr[ny][nx] = irland q.append((nx, ny)) visited[ny][nx] = 1 def get_min_d.. 2019. 10. 17.