baekjoon 20260427

This commit is contained in:
songyc macbook 2026-04-27 22:05:36 +09:00
parent b0fd507dff
commit 1787924df7
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,29 @@
import sys
input = sys.stdin.readline
Inf = sys.maxsize
N = int(input())
W = [list(map(int, input().split())) for _ in range(N)]
dp = [[None] * (2**N) for _ in range(N)]
def trevel(now, visited) :
if visited == ((1 << N) - 1) :
return w if (w := W[now][0]) != 0 else Inf
if dp[now][visited] is not None :
return dp[now][visited]
min_cost = Inf
for nxt in range(N) :
if W[now][nxt] == 0 or (visited & (1 << nxt)) != 0 :
continue
cost = W[now][nxt] + trevel(nxt, visited | (1 << nxt))
min_cost = min(min_cost, cost)
dp[now][visited] = min_cost
return dp[now][visited]
print(trevel(0, 1))

View File

@ -0,0 +1 @@
print((lambda x, y : y + (y-x))(int(input()), int(input())))

View File

@ -0,0 +1,64 @@
from collections import deque
import sys
input = sys.stdin.readline
ans = []
def solv() :
h, w = map(int, input().split())
Map = []
Map.append("." * (w + 2))
for _ in range(h) :
Map.append("." + input().rstrip() + ".")
Map.append("." * (w + 2))
key = [False] * 26
for k in input().rstrip() :
if k != "0" :
key[ord(k) - ord("a")] = True
qu = deque()
gate = [deque() for _ in range(26)]
qu.append((0,0))
visited = [[False] * (w + 2) for _ in range(h + 2)]
res = 0
while qu :
cx, cy = qu.popleft()
for dx, dy in zip([1,-1,0,0], [0,0,1,-1]) :
nx, ny = cx + dx, cy + dy
if nx < 0 or nx >= w+2 or ny < 0 or ny >= h+2 or visited[ny][nx] or Map[ny][nx] == "*" :
continue
ch = Map[ny][nx]
if ord("A") <= ord(ch) <= ord("Z") :
if not key[ord(ch) - ord("A")] :
gate[ord(ch) - ord("A")].append((nx, ny))
else :
qu.append((nx, ny))
elif ord("a") <= ord(ch) <= ord("z") :
qu.append((nx, ny))
key[ord(ch) - ord("a")] = True
while gate[ord(ch) - ord("a")] :
qu.append(gate[ord(ch) - ord("a")].popleft())
elif ch == "$" :
res += 1
qu.append((nx, ny))
else :
qu.append((nx, ny))
visited[ny][nx] = True
ans.append(res)
for _ in range(int(input())) :
solv()
print("\n".join(map(str, ans)))