-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
734 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sys | ||
from collections import deque | ||
|
||
def input(): return sys.stdin.readline() | ||
|
||
N, K = map(int, input().split()) | ||
|
||
visited = [0] * 100001 | ||
time = 0 | ||
parent = [-1] * 100001 | ||
|
||
q = deque([N]) | ||
visited[N] = 1 | ||
|
||
while q: | ||
size = len(q) | ||
for _ in range(size): | ||
location = q.popleft() | ||
if location == K: | ||
print(time) | ||
answer = [] | ||
while location != -1: | ||
answer.append(location) | ||
location = parent[location] | ||
answer.reverse() | ||
print(" ".join(map(str, answer))) | ||
break | ||
for next_location in (location + 1, location - 1, location * 2): | ||
if 0 <= next_location <= 100000 and not visited[next_location]: | ||
q.append(next_location) | ||
visited[next_location] = 1 | ||
parent[next_location] = location | ||
time += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N = int(input()) | ||
INF = 1000*1000+1 | ||
|
||
house = [[int(x) for x in input().split()] for _ in range(N)] | ||
cost = INF | ||
|
||
for first_house in range(3): | ||
dp = [[INF] * 3 for _ in range(N)] | ||
dp[0][first_house] = house[0][first_house] | ||
for i in range(1, N): | ||
dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + house[i][0] | ||
dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + house[i][1] | ||
dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + house[i][2] | ||
dp[N-1][first_house] = INF | ||
cost = min(cost, min(dp[N-1])) | ||
|
||
print(cost) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
def eat(row, col): | ||
row_cnt = 0 | ||
col_cnt = 0 | ||
tmp_col = 1 | ||
tmp_row = 1 | ||
for idx in range(N-1): | ||
# 열에서 먹을 때 | ||
if data[row][idx] == data[row][idx+1]: | ||
tmp_col += 1 | ||
col_cnt = max(col_cnt, tmp_col) | ||
else: | ||
tmp_col = 1 | ||
|
||
# 행에서 먹을 때 | ||
if data[idx][col] == data[idx+1][col]: | ||
tmp_row += 1 | ||
row_cnt = max(row_cnt, tmp_row) | ||
else: | ||
tmp_row = 1 | ||
|
||
return max(row_cnt, col_cnt) | ||
|
||
|
||
N = int(input()) | ||
data = [[x for x in input()] for _ in range(N)] | ||
directions = [(0,-1), (0,1), (-1,0),(1,0)] | ||
cnt = 0 | ||
|
||
for i in range(N): | ||
for k in range(N): | ||
for dir in directions: | ||
dx, dy = dir | ||
x = i+dx | ||
y = k+dy | ||
if 0<=x<N and 0<=y<N: | ||
data[i][k], data[x][y] = data[x][y], data[i][k] | ||
cnt = max(cnt, eat(x,y)) | ||
data[i][k], data[x][y] = data[x][y], data[i][k] #다시 되돌리기 | ||
|
||
print(cnt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
|
||
DIV = 1_000_000_000 | ||
|
||
N = int(sys.stdin.readline().rstrip()) | ||
|
||
dp = [[0] * 10 for _ in range(N+1)] | ||
answer = 0 | ||
|
||
for i in range(1,N+1): | ||
if i == 1: | ||
for k in range(1,10): | ||
dp[i][k] = 1 | ||
|
||
else: | ||
for num in range(10): | ||
if num == 0: | ||
dp[i][num] = dp[i-1][1]%DIV | ||
elif num == 9: | ||
dp[i][num] = dp[i-1][8]%DIV | ||
else: | ||
dp[i][num] = (dp[i-1][num-1] + dp[i-1][num+1])%DIV | ||
|
||
for k in range(10): | ||
answer += dp[N][k] | ||
|
||
print(answer%DIV) | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
stdin = open(0) | ||
|
||
def input(): | ||
return stdin.readline().rstrip() | ||
|
||
N, M = map(int, input().split()) | ||
|
||
prices = {} | ||
|
||
for _ in range(N): | ||
item, price = input().split() | ||
prices[item] = int(price) | ||
|
||
recipes = [] | ||
|
||
for _ in range(M): | ||
target, formula = input().split('=') | ||
terms = formula.split('+') | ||
recipes.append([target, terms]) | ||
|
||
def updatePrice(target, terms): | ||
price = 0 | ||
|
||
for term in terms: | ||
count = int(term[0]) | ||
item = term[1:] | ||
|
||
if item not in prices: | ||
return | ||
|
||
price += count * prices[item] | ||
|
||
if target not in prices or prices[target] > price: | ||
prices[target] = price | ||
|
||
for _ in range(M): | ||
for recipe in recipes: | ||
updatePrice(recipe[0], recipe[1]) | ||
|
||
if 'LOVE' not in prices: | ||
print('-1') | ||
exit() | ||
|
||
answer = min(prices['LOVE'], 1000000001) | ||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from collections import * | ||
|
||
map_size = int(input()) | ||
matrix = [list(map(int, input().split())) for _ in range(map_size)] | ||
|
||
directions = [(1, 0), (0, -1), (-1, 0), (0, 1)] | ||
island_id = 2 | ||
costs = [[0] * map_size for _ in range(map_size)] | ||
|
||
# 다리를 놓을 위치를 저장할 큐 | ||
bridge_queue = deque() | ||
|
||
# 전체 맵을 순회하면서 각 섬에 ID 부여 | ||
for row in range(map_size): | ||
for col in range(map_size): | ||
|
||
if matrix[row][col] != 1: | ||
continue | ||
|
||
# 현재 칸을 시작으로 하는 섬에 고유 ID 할당 | ||
matrix[row][col] = island_id | ||
queue = deque([(row, col)]) | ||
|
||
# BFS를 통해 같은 섬인 육지에 ID 할당 | ||
while queue: | ||
r, c = queue.popleft() | ||
for dr, dc in directions: | ||
nr, nc = r + dr, c + dc | ||
|
||
if not 0 <= nr < map_size or not 0 <= nc < map_size: | ||
continue | ||
|
||
# 다른 섬과 연결된 바다 영역에 접근하면 종료 | ||
if 1 < matrix[nr][nc] < island_id: | ||
print(1) | ||
exit() | ||
|
||
# 새로운 육지 발견 시 큐에 추가 | ||
if matrix[nr][nc] == 1: | ||
queue.append((nr, nc)) | ||
# 육지와 바로 맞닿은 바다 영역을 다리 후보로 추가 | ||
elif matrix[nr][nc] == costs[nr][nc] == 0: | ||
bridge_queue.append((nr, nc, 1, island_id)) | ||
|
||
costs[nr][nc] = 1 | ||
matrix[nr][nc] = island_id | ||
|
||
island_id += 1 | ||
|
||
min_cost = int(1e6) | ||
|
||
# 다리 후보 위치를 순회하며 최소 다리 길이 계산 | ||
while bridge_queue: | ||
r, c, cost, curr_island_id = bridge_queue.popleft() | ||
|
||
for dr, dc in directions: | ||
nr, nc = r + dr, c + dc | ||
|
||
if not 0 <= nr < map_size or not 0 <= nc < map_size: | ||
continue | ||
|
||
# 아직 다리가 놓이지 않은 바다 영역이면 | ||
# 다리 길이를 증가시키고 큐에 추가 | ||
if costs[nr][nc] == 0: | ||
bridge_queue.append((nr, nc, cost + 1, curr_island_id)) | ||
|
||
costs[nr][nc] = cost + 1 | ||
matrix[nr][nc] = curr_island_id | ||
# 다른 섬과 연결된 다리 영역에 접근하였다면 최소 비용을 갱신 | ||
elif matrix[nr][nc] > 1 and matrix[nr][nc] != curr_island_id: | ||
min_cost = min(min_cost, cost + costs[nr][nc]) | ||
|
||
print(min_cost) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 자물쇠의 중간 부분이 모두 1인지 확인 | ||
def is_valid(new_lock): | ||
length = len(new_lock) // 3 | ||
|
||
for r in range(length, length * 2): | ||
for c in range(length, length * 2): | ||
if new_lock[r][c] != 1: | ||
return False | ||
|
||
return True | ||
|
||
def solution(key, lock): | ||
n = len(lock) | ||
k = len(key) | ||
new_lock = [[0] * (n * 3) for _ in range(n * 3)] | ||
|
||
for r in range(n): | ||
for c in range(n): | ||
new_lock[r + n][c + n] = lock[r][c] | ||
|
||
for _ in range(4): | ||
rev_key = key[::-1] | ||
key = [] | ||
for c in range(k): | ||
row = [] | ||
for r in range(k): | ||
row.append(rev_key[r][c]) | ||
key.append(row) | ||
|
||
""" | ||
열쇠를 돌리는 로직은 한 줄로도 구현가능 합니다 | ||
key = [row for row in zip(*reversed(key))] | ||
""" | ||
|
||
for r in range(n * 2): | ||
for c in range(n * 2): | ||
# 자물쇠에 열쇠를 끼운다 | ||
for i in range(k): | ||
for j in range(k): | ||
new_lock[r + i][c + j] += key[i][j] | ||
|
||
# 자물쇠에 열쇠가 딱 들어갔는지 확인 | ||
if is_valid(new_lock): | ||
return True | ||
|
||
# 자물쇠에서 열쇠를 빼서 복구시킨다 | ||
for i in range(k): | ||
for j in range(k): | ||
new_lock[r + i][c + j] -= key[i][j] | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sys import * | ||
|
||
N = int(stdin.readline()) | ||
K = int(stdin.readline()) | ||
SENSORS = sorted(set(map(int, stdin.readline().split()))) | ||
|
||
if K >= N: | ||
print(0) | ||
exit() | ||
|
||
distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))] | ||
distances.sort(reverse=True) | ||
|
||
result = 0 | ||
for i in range(K - 1, len(distances)): | ||
result += distances[i] | ||
|
||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
N = int(input()) | ||
weights = list(map(int, input().split())) | ||
weights.sort() | ||
|
||
x = 0 | ||
for weight in weights: | ||
# x + 1이 현재 추의 무게보다 작다면, 측정이 불가능함 | ||
if x + 1 < weight: | ||
break | ||
|
||
x += weight | ||
|
||
print(x + 1) |
Oops, something went wrong.