Skip to content

Commit f642330

Browse files
committed
'09_26'
1 parent 02ac731 commit f642330

File tree

5 files changed

+110
-29
lines changed

5 files changed

+110
-29
lines changed

‎cmkim/BaekJoon/백준_1504.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
import heapq
3+
input = sys.stdin.readline
4+
INF = 1e9
5+
6+
n, e = map(int, input().split())
7+
8+
graph = [[] for i in range(n + 1)]
9+
10+
11+
arr = [[] for _ in range(n+1)]
12+
for _ in range(e):
13+
a, b, c = map(int, input().split())
14+
arr[a].append((b, c)) # a부터 b까지의 거리가 c이다.
15+
arr[b].append((a, c))
16+
v1, v2 = map(int, input().split())
17+
18+
def dijkstra(start):
19+
distance = [INF] * (n + 1)
20+
q = []
21+
heapq.heappush(q, (0, start))
22+
distance[start] = 0
23+
while q:
24+
dist, now = heapq.heappop(q)
25+
if distance[now] < dist:
26+
continue
27+
for i in arr[now]:
28+
cost = dist + i[1]
29+
if cost < distance[i[0]]:
30+
distance[i[0]] = cost
31+
heapq.heappush(q, (cost, i[0]))
32+
return distance
33+
34+
first = dijkstra(1)
35+
second = dijkstra(v1)
36+
third = dijkstra(v2)
37+
if min(first[v1]+second[v2]+third[n], first[v2]+third[v1]+second[n])<INF:
38+
print(min(first[v1]+second[v2]+third[n], first[v2]+third[v1]+second[n]))
39+
else:
40+
print('-1')

‎cmkim/BaekJoon/백준_17090.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import sys
2-
sys.setrecursionlimit(10**9)
2+
3+
sys.setrecursionlimit(10 ** 9)
34

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

7+
68
def dfs(x, y):
7-
if 0 > x or x >= n or 0 > y or y >= m: #탈출
9+
if 0 > x or x >= n or 0 > y or y >= m: # 탈출
810
return True
911

10-
if arr[x][y] == 'true':# 탈출 확정된 곳
12+
if arr[x][y] == 'true': # 탈출 확정된 곳
1113
return True
12-
elif arr[x][y] == 'false':# 탈출 불가능 확정된 곳
14+
elif arr[x][y] == 'false': # 탈출 불가능 확정된 곳
1315
return False
1416

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

2830
n, m = map(int, input().split())
2931
arr = []
30-
visited = [[0]*m for _ in range(n)]
32+
visited = [[0] * m for _ in range(n)]
3133
for i in range(n):
3234
arr.append(list(input()))
3335

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

4648
print(count)
47-

‎cmkim/BaekJoon/백준_2206.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
dx = [-1, 1, 0, 0]
6+
dy = [0, 0, -1, 1]
7+
n, m = map(int, input().split())
8+
arr = [] # 맵 정보
9+
arr.append([1 for _ in range(m + 1)])
10+
visited = [[[0] * (m + 1) for _ in range(n + 1)] for __ in range(2)]
11+
12+
for i in range(1, n + 1):
13+
arr.append(list(map(int, input().strip())))
14+
arr[i].insert(0, 1)
15+
16+
17+
def bfs():
18+
q = deque()
19+
q.append((1, 1, 1, 0)) # x, y, cost, 벽부순지에 대한 여부
20+
visited[0][1][1] = 1
21+
visited[1][1][1] = 1
22+
while q:
23+
x, y, c, flag = q.popleft()
24+
# visited[0][x][y] = c
25+
# visited[1][x][y] = c <- 이렇게 두개의 배열다 초기화 시키는 방법은 왜 시간초과가 날까?
26+
# if x == n and y == m:
27+
# print(visited[flag][x][y], flag)
28+
for i in range(4):
29+
nx = x + dx[i]
30+
ny = y + dy[i]
31+
if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 0 and not visited[flag][nx][ny]:
32+
q.append((nx, ny, c + 1, flag))
33+
visited[flag][nx][ny] = visited[flag][x][y] + 1
34+
if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 1 and flag == 0:
35+
q.append((nx, ny, c + 1, 1))
36+
visited[flag+1][nx][ny] = visited[flag][x][y] + 1
37+
38+
39+
bfs()
40+
41+
result = 0
42+
for i in range(2):
43+
if visited[i][n][m]:
44+
result = visited[i][n][m]
45+
if result:
46+
print(result)
47+
else:
48+
print(-1)

‎cmkim/BaekJoon/백준_4386.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import sys, math, heapq
2+
23
n = int(input())
34

45
arr = []
56
connected = []
67
sum = 0
78
cost = 1e9
89

10+
911
def dist(x1, y1, x2, y2):
10-
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
12+
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
13+
1114

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

36-
print("%.2f"%(sum))
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
39+
print("%.2f" %(sum))

‎cmkim/BaekJoon/백준_5547.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import sys
22
from collections import deque
3+
34
input = sys.stdin.readline
45

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

9+
810
def bfs(x, y):
911
q = deque([(x, y)])
1012
count = 0
1113
while q:
1214
x, y = q.popleft()
1315
visited[x][y] = 1
14-
dir = even if x%2 == 0 else odd
16+
dir = even if x % 2 == 0 else odd
1517
for i in range(6):
1618
dx, dy = dir[i][0], dir[i][1]
1719
nx, ny = x + dx, y + dy
1820

19-
if 0 <= nx < h+2 and 0 <= ny < w+2:
21+
if 0 <= nx < h + 2 and 0 <= ny < w + 2:
2022
if arr[nx][ny] == 1:
2123
count += 1
2224
elif arr[nx][ny] == 0 and not visited[nx][ny]:
@@ -25,13 +27,12 @@ def bfs(x, y):
2527
return count
2628

2729

28-
29-
w, h = map(int, input().split()) # 열, 행 순서로 입력
30-
arr = [[0] * (w+2)]
31-
visited = [[0] * (w+2) for _ in range(h+2)]
30+
w, h = map(int, input().split()) # 열, 행 순서로 입력
31+
arr = [[0] * (w + 2)]
32+
visited = [[0] * (w + 2) for _ in range(h + 2)]
3233
for i in range(h):
33-
arr.append([0]+list(map(int, input().split())) + [0])
34-
arr.append([0 for _ in range(w+2)])
34+
arr.append([0] + list(map(int, input().split())) + [0])
35+
arr.append([0 for _ in range(w + 2)])
3536

3637
# for i in range(h+2):
3738
# print(arr[i])

0 commit comments

Comments
 (0)