Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cmkim/BaekJoon/백준_1504.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import heapq
input = sys.stdin.readline
INF = 1e9

n, e = map(int, input().split())

graph = [[] for i in range(n + 1)]


arr = [[] for _ in range(n+1)]
for _ in range(e):
a, b, c = map(int, input().split())
arr[a].append((b, c)) # a부터 b까지의 거리가 c이다.
arr[b].append((a, c))
v1, v2 = map(int, input().split())

def dijkstra(start):
distance = [INF] * (n + 1)
q = []
heapq.heappush(q, (0, start))
distance[start] = 0
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for i in arr[now]:
cost = dist + i[1]
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
return distance

first = dijkstra(1)
second = dijkstra(v1)
third = dijkstra(v2)
if min(first[v1]+second[v2]+third[n], first[v2]+third[v1]+second[n])<INF:
print(min(first[v1]+second[v2]+third[n], first[v2]+third[v1]+second[n]))
else:
print('-1')
13 changes: 7 additions & 6 deletions cmkim/BaekJoon/백준_17090.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import sys
sys.setrecursionlimit(10**9)

sys.setrecursionlimit(10 ** 9)

dir = {'U': (-1, 0), 'R': (0, 1), 'D': (1, 0), 'L': (0, -1)}


def dfs(x, y):
if 0 > x or x >= n or 0 > y or y >= m: #탈출
if 0 > x or x >= n or 0 > y or y >= m: # 탈출
return True

if arr[x][y] == 'true':# 탈출 확정된 곳
if arr[x][y] == 'true': # 탈출 확정된 곳
return True
elif arr[x][y] == 'false':# 탈출 불가능 확정된 곳
elif arr[x][y] == 'false': # 탈출 불가능 확정된 곳
return False

if visited[x][y]:
Expand All @@ -27,7 +29,7 @@ def dfs(x, y):

n, m = map(int, input().split())
arr = []
visited = [[0]*m for _ in range(n)]
visited = [[0] * m for _ in range(n)]
for i in range(n):
arr.append(list(input()))

Expand All @@ -44,4 +46,3 @@ def dfs(x, y):
count += 1

print(count)

48 changes: 48 additions & 0 deletions cmkim/BaekJoon/백준_2206.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sys
from collections import deque

input = sys.stdin.readline
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
n, m = map(int, input().split())
arr = [] # 맵 정보
arr.append([1 for _ in range(m + 1)])
visited = [[[0] * (m + 1) for _ in range(n + 1)] for __ in range(2)]

for i in range(1, n + 1):
arr.append(list(map(int, input().strip())))
arr[i].insert(0, 1)


def bfs():
q = deque()
q.append((1, 1, 1, 0)) # x, y, cost, 벽부순지에 대한 여부
visited[0][1][1] = 1
visited[1][1][1] = 1
while q:
x, y, c, flag = q.popleft()
# visited[0][x][y] = c
# visited[1][x][y] = c <- 이렇게 두개의 배열다 초기화 시키는 방법은 왜 시간초과가 날까?
# if x == n and y == m:
# print(visited[flag][x][y], flag)
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 0 and not visited[flag][nx][ny]:
q.append((nx, ny, c + 1, flag))
visited[flag][nx][ny] = visited[flag][x][y] + 1
if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 1 and flag == 0:
q.append((nx, ny, c + 1, 1))
visited[flag+1][nx][ny] = visited[flag][x][y] + 1


bfs()

result = 0
for i in range(2):
if visited[i][n][m]:
result = visited[i][n][m]
if result:
print(result)
else:
print(-1)
19 changes: 5 additions & 14 deletions cmkim/BaekJoon/백준_4386.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import sys, math, heapq

n = int(input())

arr = []
connected = []
sum = 0
cost = 1e9


def dist(x1, y1, x2, y2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)


for i in range(n):
x, y = map(float, input().split())
Expand All @@ -33,16 +36,4 @@ def dist(x1, y1, x2, y2):
if i not in connected:
heapq.heappush(q, (dist(arr[node][0], arr[node][1], arr[i][0], arr[i][1]), i))

print("%.2f"%(sum))












print("%.2f" %(sum))
19 changes: 10 additions & 9 deletions cmkim/BaekJoon/백준_5547.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import sys
from collections import deque

input = sys.stdin.readline

odd = [(-1, 1), (0, 1), (1, 1), (1, 0), (0, -1), (-1, 0)] #행, 렬 기준으로함 정각부터 정시계방향으로 순환
odd = [(-1, 1), (0, 1), (1, 1), (1, 0), (0, -1), (-1, 0)] # 행, 렬 기준으로함 정각부터 정시계방향으로 순환
even = [(-1, 0), (0, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def bfs(x, y):
q = deque([(x, y)])
count = 0
while q:
x, y = q.popleft()
visited[x][y] = 1
dir = even if x%2 == 0 else odd
dir = even if x % 2 == 0 else odd
for i in range(6):
dx, dy = dir[i][0], dir[i][1]
nx, ny = x + dx, y + dy

if 0 <= nx < h+2 and 0 <= ny < w+2:
if 0 <= nx < h + 2 and 0 <= ny < w + 2:
if arr[nx][ny] == 1:
count += 1
elif arr[nx][ny] == 0 and not visited[nx][ny]:
Expand All @@ -25,13 +27,12 @@ def bfs(x, y):
return count



w, h = map(int, input().split()) # 열, 행 순서로 입력
arr = [[0] * (w+2)]
visited = [[0] * (w+2) for _ in range(h+2)]
w, h = map(int, input().split()) # 열, 행 순서로 입력
arr = [[0] * (w + 2)]
visited = [[0] * (w + 2) for _ in range(h + 2)]
for i in range(h):
arr.append([0]+list(map(int, input().split())) + [0])
arr.append([0 for _ in range(w+2)])
arr.append([0] + list(map(int, input().split())) + [0])
arr.append([0 for _ in range(w + 2)])

# for i in range(h+2):
# print(arr[i])
Expand Down