Skip to content

Commit 41f19ff

Browse files
authored
Merge pull request #52 from YoonYn9915/main
YoonYn9915 / 8월1주차/ 3문제
2 parents 50036c8 + 8a60e91 commit 41f19ff

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
from collections import deque
3+
4+
#1. 입력값 받기
5+
#2. 모든 배열을 돌며 인구이동 조건을 만족하는 나라가 있는지 확인. (1. 인접한 나라 2. 두 국가의 인구 차의 절댓값이 l이상 r이하)
6+
#3. 인구 이동조건을 만족하는 나라가 있으면 bfs로 그래프를 탐색하면서 나라의 개수와 인구의 총합을 구해서 새로운 인구를 구해준다.
7+
#4. visited 배열은 인구 이동날마다 초기화 해야 한다.
8+
9+
# 실제로 인구를 이동시키는 함수
10+
def movePopulation(i, j):
11+
global n, l, r
12+
global visited, arr
13+
14+
dx = [-1, 1, 0, 0]
15+
dy = [0, 0, -1, 1]
16+
17+
queue = deque([(i, j)])
18+
visited[i][j] = 1
19+
queue2 = deque([(i, j)])
20+
populationSum = arr[i][j]
21+
countrySum = 1
22+
23+
while queue:
24+
x, y = queue.popleft()
25+
26+
for d in range(4):
27+
row = x + dx[d]
28+
col = y + dy[d]
29+
30+
if 0 <= row < n and 0 <= col < n and visited[row][col] == 0 and l <= abs(arr[x][y] - arr[row][col]) <= r:
31+
visited[row][col] = 1
32+
countrySum += 1
33+
populationSum += arr[row][col]
34+
queue.append((row, col))
35+
queue2.append((row, col))
36+
37+
newPopulation = populationSum // countrySum
38+
39+
while queue2:
40+
x, y = queue2.popleft()
41+
arr[x][y] = newPopulation
42+
43+
# 인구 이동 조건을 만족하는 나라가 있는지 확인하는 함수
44+
def checkCountries():
45+
global ans
46+
47+
dx = [-1, 1, 0, 0]
48+
dy = [0, 0, -1, 1]
49+
50+
for i in range(n):
51+
for j in range(n):
52+
for k in range(4):
53+
row = i + dx[k]
54+
col = j + dy[k]
55+
if 0 <= row < n and 0 <= col < n and l <= abs(arr[i][j] - arr[row][col]) <= r:
56+
ans += 1
57+
return True
58+
59+
return False
60+
61+
62+
inp = sys.stdin.readline
63+
64+
n, l, r = map(int, inp().split())
65+
66+
arr = []
67+
visited = [[0] * n for _ in range(n)]
68+
69+
ans = 0
70+
71+
for i in range(n):
72+
arr.append(list(map(int, inp().split())))
73+
74+
while checkCountries():
75+
for i in range(n):
76+
for j in range(n):
77+
if visited[i][j] == 0:
78+
movePopulation(i, j)
79+
visited = [[0] * n for _ in range(n)]
80+
81+
print(ans)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 1. 거스름돈의 개수가 최소여야 하므로 greedy 알고리즘을 이용해서 단위가 큰 쿼터부터 계산해준다.
2+
3+
4+
import sys
5+
6+
7+
def compute(item):
8+
ans = [0] * 4
9+
global quarter, dime, nickel, penny
10+
11+
while item != 0:
12+
if item - quarter >= 0:
13+
item -= quarter
14+
ans[0] += 1
15+
continue
16+
if item - dime >= 0:
17+
item -= dime
18+
ans[1] += 1
19+
continue
20+
if item - nickel >= 0:
21+
item -= nickel
22+
ans[2] += 1
23+
continue
24+
if item - penny >= 0:
25+
item -= penny
26+
ans[3] += 1
27+
continue
28+
return ans
29+
30+
31+
inp = sys.stdin.readline
32+
33+
n = int(inp())
34+
arr = []
35+
36+
quarter = 25
37+
dime = 10
38+
nickel = 5
39+
penny = 1
40+
41+
for i in range(n):
42+
arr.append(int(inp()))
43+
44+
for item in arr:
45+
answers = compute(item)
46+
for ans in answers:
47+
print(ans, end=" ")
48+
print()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 재귀적으로 정사각형을 4분할한다.
2+
# 재귀 시작시 ( 출력, 종료시 ) 출력
3+
# 재귀 종료조건은 정사각형의 모든 값이 같거나, 정사각형이 길이가 1일때
4+
5+
import sys
6+
7+
8+
def recursion(arr, n, row, col):
9+
global ans
10+
11+
if n == 1:
12+
ans.append(arr[row][col])
13+
return
14+
15+
flag = 0
16+
first = arr[row][col]
17+
for i in range(n):
18+
for j in range(n):
19+
if arr[row + i][col + j] != first:
20+
flag = 1
21+
break
22+
23+
if flag == 0:
24+
ans.append(first)
25+
return
26+
else:
27+
ans.append('(')
28+
recursion(arr, n // 2, row, col)
29+
recursion(arr, n // 2, row, col + n // 2)
30+
recursion(arr, n // 2, row + n // 2, col)
31+
recursion(arr, n // 2, row + n // 2, col + n // 2)
32+
ans.append(')')
33+
34+
35+
36+
inp = sys.stdin.readline
37+
38+
n = int(inp())
39+
arr = []
40+
41+
for _ in range(n):
42+
arr.append(inp().strip())
43+
44+
ans = []
45+
46+
recursion(arr, n, 0, 0)
47+
48+
for i in range(len(ans)):
49+
print(ans[i], end="")

0 commit comments

Comments
 (0)