Skip to content

Commit a1ca274

Browse files
authored
Merge pull request #105 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 12์›” 2์ฃผ์ฐจ / 4๋ฌธ์ œ
2 parents 229bb5d + a41e29c commit a1ca274

4 files changed

+122
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
n = int(input())
2+
3+
num = [[0]*10 for _ in range(n+1)]
4+
num[0] = [1,1,1,1,1,1,1,1,0] #0ํ–‰์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’๋“ค์„ ๊ณ„๋‹จ์ˆ˜์˜ ๊ฒฝ์šฐ์˜์ˆ˜๋กœ ์ดˆ๊ธฐํ™”
5+
6+
for i in range(1,n+1):
7+
for j in range(10): #0์ผ๋•Œ, 9์ผ๋•Œ, ๋‚˜๋จธ์ง€์ธ ๊ฒฝ์šฐ๋ฅผ ์ ํ™”์‹์„ ํ† ๋Œ€๋กœ ์ฝ”๋“œ ์ž‘์„ฑ
8+
if j == 0:
9+
num[i][j] = num[i-1][1]
10+
elif j == 9:
11+
num[i][j] = num[i-1][8]
12+
else:
13+
num[i][j] = num[i-1][j-1] + num[i-1][j]
14+
15+
answer = sum(num[n]) % 100000000
16+
print(answer)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# ํ•˜๋…ธ์ด ํ•จ์ˆ˜
2+
def hanoi_f(one, three, n):
3+
if n == 1:
4+
print(one, three)
5+
return
6+
7+
hanoi_f(one, 6 - one - three, n - 1) # 1๋‹จ๊ณ„ (1->2)
8+
print(one, three) # 2๋‹จ๊ณ„ (๋งˆ์ง€๋ง‰์›๋ฐ˜ 1->3)
9+
hanoi_f(6 - one - three, three, n - 1) # 3๋‹จ๊ณ„ (2->3)
10+
11+
12+
# ๋ฉ”์ธ
13+
n = int(input())
14+
print(2 ** n - 1)
15+
if n <= 20:
16+
hanoi_f(1, 3, n)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# ๋ˆ„์ ํ•ฉ ํ…Œ์ด๋ธ” ๊ณ„์‚ฐ
5+
def get_prefix_sum(grid, N):
6+
prefix_sum = [[0] * (N+1) for _ in range(N+1)]
7+
for i in range(1, N+1):
8+
for j in range(1, N+1):
9+
prefix_sum[i][j] = (
10+
grid[i-1][j-1]
11+
+ prefix_sum[i-1][j]
12+
+ prefix_sum[i][j-1]
13+
- prefix_sum[i-1][j-1]
14+
)
15+
return prefix_sum
16+
17+
# ์ž…๋ ฅ
18+
N, M = map(int, input().split())
19+
grid = [list(map(int, input().split())) for _ in range(N)]
20+
21+
# ๋ˆ„์ ํ•ฉ
22+
prefix_sum = get_prefix_sum(grid, N)
23+
24+
# ๋ฒ”์œ„ ํ•ฉ ๊ณ„์‚ฐ
25+
for _ in range(M):
26+
x1, y1, x2, y2 = map(int, input().split())
27+
answer = (
28+
prefix_sum[x2][y2]
29+
- prefix_sum[x1-1][y2]
30+
- prefix_sum[x2][y1-1]
31+
+ prefix_sum[x1-1][y1-1]
32+
)
33+
print(answer)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
n, m = map(int, input().split())
2+
graph = [list(map(int, input().split())) for _ in range(n)]
3+
visited = [[False] * m for _ in range(n)]
4+
5+
# ๋ฐฉํ–ฅ ์„ค์ • (์ƒ, ํ•˜, ์ขŒ, ์šฐ)
6+
dx = [-1, 1, 0, 0]
7+
dy = [0, 0, -1, 1]
8+
9+
# ์ตœ๋Œ“๊ฐ’ ์ €์žฅ ๋ณ€์ˆ˜
10+
max_value = 0
11+
12+
13+
# DFS ํƒ์ƒ‰ (ใ…— ๋ชจ์–‘ ์ œ์™ธ)
14+
def dfs(x, y, count, total):
15+
global max_value
16+
# ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ 4์นธ ์ฑ„์› ์„ ๊ฒฝ์šฐ
17+
if count == 4:
18+
max_value = max(max_value, total)
19+
return
20+
21+
# ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
22+
for i in range(4):
23+
nx, ny = x + dx[i], y + dy[i]
24+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
25+
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]:
26+
visited[nx][ny] = True
27+
dfs(nx, ny, count + 1, total + graph[nx][ny])
28+
visited[nx][ny] = False # ๋ฐฑํŠธ๋ž˜ํ‚น
29+
30+
31+
# ใ…— ๋ชจ์–‘ ํƒ์ƒ‰
32+
def check_t_shape(x, y):
33+
global max_value
34+
# ์ค‘์‹ฌ ๊ธฐ์ค€ 4๋ฐฉํ–ฅ ์ค‘ 3๊ฐœ๋ฅผ ์„ ํƒ (ใ…—, ใ…œ, ใ…, ใ…“)
35+
for i in range(4):
36+
total = graph[x][y]
37+
for j in range(3): # ํ˜„์žฌ ์ œ์™ธํ•œ 3๋ฐฉํ–ฅ ํƒ์ƒ‰
38+
k = (i + j) % 4
39+
nx, ny = x + dx[k], y + dy[k]
40+
if 0 <= nx < n and 0 <= ny < m:
41+
total += graph[nx][ny]
42+
else: # ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ใ…— ๋ชจ์–‘์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์Œ
43+
break
44+
else: # ๋ชจ๋“  3๋ฐฉํ–ฅ ํƒ์ƒ‰์ด ์œ ํšจํ•œ ๊ฒฝ์šฐ
45+
max_value = max(max_value, total)
46+
47+
48+
# ๋ชจ๋“  ์ขŒํ‘œ์—์„œ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํƒ์ƒ‰
49+
for i in range(n):
50+
for j in range(m):
51+
visited[i][j] = True
52+
dfs(i, j, 1, graph[i][j]) # DFS ์‹œ์ž‘
53+
visited[i][j] = False
54+
check_t_shape(i, j) # ใ…— ๋ชจ์–‘ ํƒ์ƒ‰
55+
56+
# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
57+
print(max_value)

0 commit comments

Comments
ย (0)