Skip to content

Commit 322990d

Browse files
authored
찬민 dfs/bfs 백준 숙제 (#57)
* 찬민 dfs/bfs 백준 숙제 * 무지의 먹방라이브
1 parent 63a6d0b commit 322990d

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

cmkim/BaekJoon/백준_11724.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
n, m = map(int, input().split())
2+
arr = [[0] * (n + 1) for _ in range(n + 1)]
3+
visited = [0] * (n + 1)
4+
result = 0
5+
6+
def dfs(num):
7+
visited[num] = 1
8+
9+
for i in range(1, n+1):
10+
if visited[i] == 0 and arr[num][i] == 1:
11+
visited[i] = 1
12+
dfs(i)
13+
return True
14+
15+
16+
17+
for i in range(m): #연결리스트
18+
a, b = map(int, input().split())
19+
arr[a][b] = 1
20+
arr[b][a] = 1
21+
22+
23+
for i in range(1, n + 1):
24+
if visited[i] == 0:
25+
dfs(i)
26+
result += 1
27+
print(result)
28+

cmkim/BaekJoon/백준_2667.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from collections import deque
2+
3+
n = int(input())
4+
arr = [[0] * n for _ in range(n)]
5+
visited = [[0] * n for _ in range(n)]
6+
dx, dy = [-1, 1, 0 , 0], [0, 0, -1, 1] # 상하좌우 순
7+
answer = []
8+
9+
def bfs():
10+
count = 1
11+
while q:
12+
x, y = q.popleft() # x는 row, y는 col
13+
visited[x][y] = 1
14+
for i in range(4):
15+
nx = x + dx[i]
16+
ny = y + dy[i]
17+
if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == 0 and arr[nx][ny] == 1:
18+
visited[nx][ny] = 1
19+
q.append((nx, ny))
20+
count += 1
21+
return count
22+
23+
24+
for i in range(n):
25+
arr[i] = list(map(int, input().rstrip()))
26+
27+
q = deque()
28+
for i in range(n):
29+
for j in range(n):
30+
if arr[i][j] and visited[i][j] == 0:
31+
q.append((i, j))
32+
result = bfs()
33+
if result:
34+
answer.append(result)
35+
36+
print(len(answer))
37+
answer.sort()
38+
for i in range(len(answer)):
39+
print(answer[i])
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# https://programmers.co.kr/learn/courses/30/lessons/42891
2+
import copy
3+
4+
5+
def solution(food_times, k):
6+
answer = 0
7+
arr = []
8+
arr2 = []
9+
arr = copy.deepcopy(food_times)
10+
cost = [0] * len(arr)
11+
accumulate_cost = [0] * len(arr)
12+
13+
arr.sort()
14+
15+
cur = 0
16+
count = 0
17+
sum = 0
18+
19+
for i in range(len(arr)):
20+
if cur != arr[i]:
21+
cost[i] = (len(arr) - count) * (arr[i] - sum)
22+
cur = arr[i]
23+
sum = cur
24+
for j in range(i, len(arr)):
25+
if arr[i] == arr[j]:
26+
count += 1
27+
28+
# print(cost)
29+
30+
accumulate_cost[0] = cost[0]
31+
for i in range(1, len(arr)):
32+
accumulate_cost[i] = accumulate_cost[i - 1] + cost[i]
33+
maxac = accumulate_cost[i]
34+
# print(accumulate_cost)
35+
36+
n = 0
37+
for i in range(len(arr)):
38+
n = accumulate_cost[i]
39+
if n > k:
40+
# print('i = ', i)
41+
n = arr[i - 1]
42+
m = k - accumulate_cost[i - 1]
43+
break
44+
# print('k = ',k)
45+
# print('maxac = ', maxac)
46+
47+
if k >= maxac:
48+
# print('Overflow')
49+
return -1
50+
51+
# m = k - accumulate_cost[n]
52+
53+
# print('n = ' , n)
54+
# print('m = ' , m)
55+
56+
# print(food_times)
57+
58+
cnt = 0
59+
for i in range(len(arr)):
60+
if food_times[i] > n:
61+
arr2.append(food_times[i])
62+
63+
length = len(arr2)
64+
65+
for i in range(len(arr)):
66+
if food_times[i] > n:
67+
m %= length
68+
# print('cnt = ', cnt)
69+
# print('foodtimes = ', food_times[i])
70+
if length >= m:
71+
if cnt == m:
72+
# print('i = ', i)
73+
answer = i + 1
74+
break
75+
cnt += 1
76+
77+
# print(answer)
78+
79+
return answer

cmkim/문자열뒤집기_313p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
temp = arr[0][i]
1010
count += 1
1111

12-
print(count//2)
12+
print((count+1)//2)

0 commit comments

Comments
 (0)