Skip to content

Commit 3ccaa65

Browse files
authored
Merge pull request #47 from 24-Algorithm/minjeong
Minjeong / 7월 4주차 / 8문제
2 parents f221616 + 2723e4d commit 3ccaa65

9 files changed

+222
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
def bfs(start):
7+
global k
8+
queue = deque([start])
9+
visited[start] = 0 # 시작 노드의 거리를 0으로 설정
10+
while queue:
11+
node = queue.popleft()
12+
for connected_node in graph[node]: # 해당 노드와 연결된 노드 순회
13+
if visited[connected_node] == -1: # 방문하지 않은 노드일 경우
14+
queue.append(connected_node) # 큐에 추가하고
15+
visited[connected_node] = visited[node] + 1 # 거리를 1 증가
16+
17+
# 연결된 노드의 거리가 k와 같다면 answer 리스트에 추가
18+
if visited[connected_node] == k:
19+
answer.append(connected_node)
20+
return answer
21+
22+
# 도시 개수 n, 도로 개수 m, 거리 정보 k, 출발도시 번호 x
23+
n, m, k, x = map(int, input().strip().split())
24+
visited = [-1 for _ in range(n + 1)] # 모든 노드를 -1로 초기화 (방문하지 않은 상태)
25+
graph = [[] for _ in range(n + 1)]
26+
answer = []
27+
28+
# 그래프 연결
29+
for _ in range(m):
30+
u, v = map(int, input().strip().split())
31+
graph[u].append(v)
32+
33+
# BFS 호출 -> 출발도시 번호 x로 시작
34+
answer = bfs(x)
35+
if not answer:
36+
print(-1)
37+
else:
38+
answer.sort()
39+
for u in answer:
40+
print(u)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 목표지점까지의 거리 구하기
6+
def find_distance_to_goal(n, m, grid):
7+
# 거리배열(모든 지점에 대한 거리를 저장)
8+
distances = [[-1 for _ in range(m)] for _ in range(n)]
9+
10+
# 목표지점 찾기
11+
for i in range(n):
12+
for j in range(m):
13+
if grid[i][j] == 2: # 목표지점의 위치 저장하고, 0으로 설정
14+
target_x = i
15+
target_y = j
16+
distances[i][j] = 0
17+
if grid[i][j] == 0: # 땅이 아닌 곳이므로 0으로 설정
18+
distances[i][j] = 0
19+
20+
queue = deque([(target_x, target_y)]) # 큐 초기화
21+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 방향 리스트
22+
23+
# BFS 탐색
24+
while queue:
25+
x, y = queue.popleft() # 현재지점 pop
26+
for dx, dy in directions:
27+
nx, ny = x + dx, y + dy # 현재 지점에서 상하좌우 인접한 지점
28+
# 인접한 지점이 지도의 범위 안에 있고, 갈 수 있는 땅(값이 1)이며, 아직 방문하지 않은 지점이라면:
29+
if (0 <= nx < n and 0 <= ny < m) and grid[nx][ny] == 1 and distances[nx][ny] == -1:
30+
distances[nx][ny] = distances[x][y] + 1 # 인접한 지점의 거리를 현재 지점의 거리 + 1로 설정
31+
queue.append((nx, ny)) # 인접한 지점을 큐에 추가
32+
33+
return distances
34+
35+
# 초기화
36+
n, m = map(int, input().split()) # n: 세로, m: 가로
37+
grid = [] # 지도
38+
for _ in range(n):
39+
line = list(map(int, input().split()))
40+
grid.append(line)
41+
42+
# 함수 호출
43+
distances = find_distance_to_goal(n, m, grid)
44+
45+
# 정답 출력
46+
for d in distances:
47+
print(" ".join(map(str, d))) # 공백을 구분하여 출력
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
def get_result(goal_sec, goal_x, goal_y, grid, n, k):
6+
virus_info = [] # 바이러스 정보를 담는 리스트 초기화
7+
8+
# 시험관을 순회하며 바이러스 팀섹
9+
for i in range(n):
10+
for j in range(n):
11+
if grid[i][j] != 0: # 바이러스라면
12+
virus_info.append((grid[i][j], 0, i, j)) # 바이러스 번호, 현재 시간, x좌표, y좌표를 추가
13+
14+
virus_info.sort() # 바이러스 정보 리스트를 오름차순으로 정렬
15+
queue = deque(virus_info) # 큐 초기화
16+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 방향 리스트 (상, 하, 좌, 우)
17+
18+
while queue:
19+
virus_num, time, x, y = queue.popleft()
20+
21+
# 목표 시간에 도달하면 종료
22+
if time == goal_sec:
23+
break
24+
25+
for dx, dy in directions:
26+
nx, ny = x + dx, y + dy # 현재 지점에서 상하좌우 인접한 지점
27+
# 시험관 범위 안에 있고 아직 바이러스가 없는 경우 현재의 바이러스를 전파
28+
if (0 <= nx < n and 0 <= ny < n) and grid[nx][ny] == 0:
29+
grid[nx][ny] = virus_num
30+
queue.append((virus_num, time + 1, nx, ny))
31+
32+
return grid[goal_x][goal_y]
33+
34+
# 입력 처리
35+
n, k = map(int, input().split()) # n: 시험관 크기 / k: 바이러스 개수
36+
grid = [] # 시험관 정보
37+
for _ in range(n):
38+
li = list(map(int, input().split()))
39+
grid.append(li)
40+
s, x, y = map(int, input().split()) # 시간, x좌표, y좌표
41+
42+
# 함수 호출 및 결과 출력
43+
print(get_result(s, x-1, y-1, grid, n, k))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def w(a, b, c):
5+
if (a, b, c) in memo:
6+
return memo[(a, b, c)]
7+
if a <= 0 or b <= 0 or c <= 0:
8+
memo[(a, b, c)] = 1
9+
return 1
10+
elif a > 20 or b > 20 or c > 20:
11+
memo[(a, b, c)] = w(20, 20, 20)
12+
elif a < b and b < c:
13+
memo[(a, b, c)] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c)
14+
else:
15+
memo[(a, b, c)] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1)
16+
return memo[(a, b, c)]
17+
18+
memo = {}
19+
while True:
20+
a, b, c = map(int, input().split())
21+
if a == -1 and b == -1 and c == -1:
22+
break
23+
print('w(%d, %d, %d) = %d' % (a, b, c, w(a, b, c)))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 초기화
5+
n = int(input()) # n: 도시의 개수
6+
road = list(map(int, input().split())) # 도로의 길이
7+
oil_price = list(map(int, input().split())) # 리터당 가격
8+
9+
min_cost = 0 # 최저 비용
10+
min_price = oil_price[0] # 첫번째 도시의 기름 가격
11+
12+
for i in range(n - 1):
13+
if oil_price[i] < min_price: # 현재 도시에서의 기름 가격이 최소 가격보다 작으면 갱신
14+
min_price = oil_price[i]
15+
min_cost += min_price * road[i] # 현재 도시에서 다음 도시로 이동할 때 드는 비용을 추가
16+
17+
print(min_cost)
18+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
from math import ceil
3+
input = sys.stdin.readline
4+
5+
n = int(input()) # 참가자 수
6+
sizes = list(map(int, input().split())) # 티셔츠사이즈별 신청자 수(S, M, L, XL, XXL, XXXL)
7+
t, p = map(int, input().split()) # t: 티셔츠 묶음 수, p: 펜 묶음 수
8+
9+
# t장씩 최소 몇 묶음 주문해야 하는지 계산
10+
min_t_shirts = 0
11+
for s in sizes:
12+
min_t_shirts += ceil(s / t)
13+
print(min_t_shirts)
14+
15+
# p자루씩 최대 몇 묶음 주문할 수 있는지와, 그 때 펜을 한 자루씩 몇 개 주문하는지 계산
16+
print(n // p, n % p)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
from math import ceil
3+
input = sys.stdin.readline
4+
5+
a = int(input())
6+
b = int(input())
7+
c = int(input())
8+
9+
print(a + b - c)
10+
print(int(str(a)+str(b))-c)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
from math import ceil
3+
input = sys.stdin.readline
4+
5+
# FizzBuzz 문자열 배열
6+
fizzbuzz = [0] * 3
7+
for i in range(3):
8+
fizzbuzz[i] = input().strip()
9+
10+
for s in fizzbuzz:
11+
if s.isdigit(): # 문자열이 숫자로 이루어져있다면,
12+
idx = fizzbuzz.index(s) # 해당 문자열의 위치를 저장하여
13+
answer = int(fizzbuzz[idx]) + (3 - idx) # 정답 값을 구하기
14+
15+
# FizzBuzz 규칙에 따라 정답 출력
16+
if answer % 3 == 0 and answer % 5 == 0:
17+
print("FizzBuzz")
18+
elif answer % 3 == 0:
19+
print("Fizz")
20+
elif answer % 5 == 0:
21+
print("Buzz")
22+
else:
23+
print(answer)
24+
25+

0 commit comments

Comments
 (0)