diff --git "a/week-32/\352\271\200\354\247\204\354\225\204/16235.py" "b/week-32/\352\271\200\354\247\204\354\225\204/16235.py" new file mode 100644 index 0000000..902ef3b --- /dev/null +++ "b/week-32/\352\271\200\354\247\204\354\225\204/16235.py" @@ -0,0 +1,114 @@ +# 16235 나무 재테크 + +from collections import deque + +def SS(): + for i in range(n): + for j in range(n): + for k in range(len(tree[i][j])): + + if food[i][j] >= tree[i][j][k]: + food[i][j] -= tree[i][j][k] + tree[i][j][k] += 1 + else: + for _ in range(k, len(tree[i][j])): + food[i][j] += tree[i][j].pop() // 2 + break + return + +def FW(): + for i in range(n): + for j in range(n): + for k in range(len(tree[i][j])): + if tree[i][j][k] % 5 == 0: + for k in range(8): + nx, ny = i + dx[k], j + dy[k] + if 0 <= nx < n and 0 <= ny < n: + tree[nx][ny].appendleft(1) + food[i][j] += a[i][j] + return + +n, m, k = map(int, input().split()) +# 로봇 +a = [list(map(int, input().split())) for _ in range(n)] +tree = [[deque() for _ in range(n)] for _ in range(n)] +food = [[5] * n for _ in range(n)] + +dx = [-1, -1, -1, 0, 0, 1, 1, 1] +dy = [-1, 0, 1, -1, 1, -1, 0, 1] + +for _ in range(m): + x, y, z = map(int, input().split()) + tree[x - 1][y - 1].append(z) + +for _ in range(k): + SS() + FW() + +answer = 0 + +for i in range(n): + answer += sum(map(len, tree[i])) + +print(answer) + +# from collections import deque +# from queue import PriorityQueue +# import heapq + +# n, m, k = map(int, input().split()) +# a = [list(map(int, input().split())) for _ in range(n)] +# tree = [] + +# dx = [-1, -1, -1, 0, 0, 1, 1, 1] +# dy = [-1, 0, 1, -1, 1, -1, 0, 1] + +# for i in range(m): +# x, y, z = map(int, input().split()) +# tree.append((x - 1, y - 1, z)) + +# # 양분 +# food = [[5 for _ in range(n)] for _ in range(n)] + +# def seasons(): +# # 봄 +# global tree, food +# tree.sort() +# deadTrees = [] + +# for i in range(len(tree)): +# if i >= len(tree): +# break +# (x, y, z) = tree[i] + + +# if z <= food[x][y]: +# food[x][y] -= z +# tree[i] = (x, y, z + 1) +# else: +# temp = tree.pop(i) +# deadTrees.append(temp) + +# # 여름 +# for (x, y, z) in deadTrees: +# food[x][y] += z // 2 + +# # 가을 +# for (x, y, z) in tree: +# if z % 5 != 0: +# continue + +# for i in range(8): +# nx, ny = x + dx[i], y + dy[i] +# if 0 <= nx < n and 0 <= ny < n: +# tree.append((nx, ny, 1)) + +# # 겨울 +# for i in range(n): +# for j in range(n): +# food[i][j] += a[i][j] + +# for i in range(k): +# seasons() + +# print(len(tree)) \ No newline at end of file diff --git "a/week-32/\352\271\200\354\247\204\354\225\204/17070.py" "b/week-32/\352\271\200\354\247\204\354\225\204/17070.py" new file mode 100644 index 0000000..a45c23b --- /dev/null +++ "b/week-32/\352\271\200\354\247\204\354\225\204/17070.py" @@ -0,0 +1,85 @@ +# # 17070 파이프 옮기기 1 + +from collections import deque +n = int(input()) +arr = [list(map(int, input().split())) for _ in range(n)] + +q = deque([(0, 1, 0)]) # 0 가로 1 세로 2 대각선 + +result = 0 + +# 끝에 거만 계산하면 되네;;; +def solution(x, y, d): + global result + if x == n - 1 and y == n - 1: + result += 1 + return + + if d == 0: + if 0 <= x < n and 0 <= y + 1 < n and arr[x][y + 1] == 0: + solution(x, y + 1, 0) + if 0 <= x + 1 < n and 0 <= y + 1 < n and arr[x + 1][y + 1] == 0 and arr[x][y + 1] == 0 and arr[x + 1][y] == 0: + solution(x + 1, y + 1, 2) + elif d == 1: + if 0 <= x + 1 < n and 0 <= y < n and arr[x + 1][y] == 0: + solution(x + 1, y, 1) + if 0 <= x + 1 < n and 0 <= y + 1 < n and arr[x + 1][y + 1] == 0 and arr[x][y + 1] == 0 and arr[x + 1][y] == 0: + solution(x + 1, y + 1, 2) + else: + if 0 <= x < n and 0 <= y + 1 < n and arr[x][y + 1] == 0: + solution(x, y + 1, 0) + if 0 <= x + 1 < n and 0 <= y < n and arr[x + 1][y] == 0: + solution(x + 1, y, 1) + if 0 <= x + 1 < n and 0 <= y + 1 < n and arr[x + 1][y + 1] == 0 and arr[x][y + 1] == 0 and arr[x + 1][y] == 0: + solution(x + 1, y + 1, 2) + +solution(0, 1, 0) +print(result) + + +# n = int(input()) +# arr = [list(map(int, input().split())) for _ in range(n)] + +# result = 0 + +# def solution(x1, y1, x2, y2): +# global result +# if x2 == n - 1 and y2 == n - 1: +# result += 1 +# return +# # 가로 +# if x1 == x2: +# for (dx1, dy1, dx2, dy2) in [(0, 1, 0, 1), (0, 1, 1, 1)]: +# nx1, ny1 = x1 + dx1, y1 + dy1 +# nx2, ny2 = x2 + dx2, y2 + dy2 + +# if 0 <= nx1 < n and 0 <= ny1 < n and 0 <= nx2 < n and 0 <= ny2 < n: +# if arr[nx1][ny1] == 1 or arr[nx2][ny2] == 1: +# continue + +# if nx1 != nx2 and ny1 != ny2: +# if arr[nx2 - 1][ny2] == 1 or arr[nx2][ny2 - 1] == 1: +# continue + +# solution(nx1, ny1, nx2, ny2) +# # 세로 +# elif y1 == y2: +# for (dx1, dy1, dx2, dy2) in [(1, 0, 1, 0), (1, 0, 1, 1)]: +# nx1, ny1 = x1 + dx1, y1 + dy1 +# nx2, ny2 = x2 + dx2, y2 + dy2 + +# if 0 <= nx1 < n and 0 <= ny1 < n and 0 <= nx2 < n and 0 <= ny2 < n: +# solution(nx1, ny1, nx2, ny2) + +# # 대각선 +# else: +# for (dx1, dy1, dx2, dy2) in [(1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]: +# nx1, ny1 = x1 + dx1, y1 + dy1 +# nx2, ny2 = x2 + dx2, y2 + dy2 + +# if 0 <= nx1 < n and 0 <= ny1 < n and 0 <= nx2 < n and 0 <= ny2 < n: +# solution(nx1, ny1, nx2, ny2) + +# solution(0, 0, 0, 1) + +# print(result) \ No newline at end of file diff --git "a/week-32/\352\271\200\354\247\204\354\225\204/17396.py" "b/week-32/\352\271\200\354\247\204\354\225\204/17396.py" new file mode 100644 index 0000000..2043af3 --- /dev/null +++ "b/week-32/\352\271\200\354\247\204\354\225\204/17396.py" @@ -0,0 +1,40 @@ +# 17396 백도어 + +import heapq +import sys +input = sys.stdin.readline + +INF = sys.maxsize +n, m = map(int, input().split()) +arr = list(map(int, input().split())) +graph = [[] for _ in range(n)] +distance = [INF] * n + +for _ in range(m): + a, b, c = map(int, input().split()) + graph[a].append((b, c)) + graph[b].append((a, c)) + +def solution(start): + q = [] + heapq.heappush(q, (0, start)) + distance[start] = 0 + + while q: + dist, now = heapq.heappop(q) + + if distance[now] < dist: + continue + + for (v, w) in graph[now]: + cost = dist + w + if cost < distance[v] and arr[now] == 0: + distance[v] = cost + heapq.heappush(q, (cost, v)) + +solution(0) + +if distance[n-1] == INF: + print(-1) +else: + print(distance[n - 1]) \ No newline at end of file diff --git "a/week-32/\352\271\200\354\247\204\354\225\204/21610.py" "b/week-32/\352\271\200\354\247\204\354\225\204/21610.py" new file mode 100644 index 0000000..77ed3f6 --- /dev/null +++ "b/week-32/\352\271\200\354\247\204\354\225\204/21610.py" @@ -0,0 +1,54 @@ + +# 21610 마법사 상어와 비바라기 + +from collections import deque + +n, m = map(int, input().split()) +a = [list(map(int, input().split())) for _ in range(n)] + +# 8방향 +dx = [0, -1, -1, -1, 0, 1, 1, 1] +dy = [-1, -1, 0, 1, 1, 1, 0, -1] + +# 대각선 확인 방향 +cx = [-1, -1, 1, 1] +cy = [-1, 1, -1, 1] + +# 구름이 있는 좌표 +cloud = deque([(n - 2, 0), (n - 2, 1), (n - 1, 0), (n - 1, 1)]) + +def magic(d, s): + prevCloud = [] + for i in range(len(cloud)): + (r, c) = cloud[i] + r = ((r + dx[d - 1] * s) + n) % n + c = ((c + dy[d - 1] * s) + n) % n + cloud[i] = (r, c) + prevCloud.append((r, c)) + + # 구름이 있는 칸에 비 내림 + a[r][c] += 1 + + + while cloud: + (r, c) = cloud.pop() + + for i in range(4): + nx = r + cx[i] + ny = c + cy[i] + + if 0 <= nx < n and 0 <= ny < n and a[nx][ny] > 0: + a[r][c] += 1 + + for i in range(n): + for j in range(n): + if a[i][j] >= 2 and (i, j) not in prevCloud: + a[i][j] -= 2 + cloud.append((i, j)) + + +for i in range(m): + d, s = map(int, input().split()) + magic(d, s) + +print(sum(map(sum, a))) diff --git "a/week-32/\352\271\200\354\247\204\354\225\204/5972.py" "b/week-32/\352\271\200\354\247\204\354\225\204/5972.py" new file mode 100644 index 0000000..b30c561 --- /dev/null +++ "b/week-32/\352\271\200\354\247\204\354\225\204/5972.py" @@ -0,0 +1,38 @@ +# 5972 택배 배송 + +import heapq +import sys +input = sys.stdin.readline +n, m = map(int, input().split()) +INF = int(1e9) + +graph = [[] for _ in range(n + 1)] +distance = [INF] * (n + 1) + +for _ in range(m): + a, b, c = map(int, input().split()) + graph[a].append((b, c)) + graph[b].append((a, c)) + + +def dijkstra(start): + q = [] + + heapq.heappush(q, (0, start)) + distance[start] = 0 + + while q: + dist, now = heapq.heappop(q) + if dist > distance[now]: + continue + + for v, w in graph[now]: + cost = dist + w + + if cost < distance[v]: + distance[v] = cost + heapq.heappush(q, (cost, v)) + +dijkstra(1) + +print(distance[n]) \ No newline at end of file