Skip to content

Commit c148235

Browse files
committed
2 parents 38a64cd + 524ec79 commit c148235

7 files changed

+271
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
N = int(input())
2+
3+
# ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„  ์ˆœ
4+
dx = [0, 1, 1]
5+
dy = [1, 0, 1]
6+
7+
8+
def dfs(x, y, dir):
9+
# ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ƒํƒœ๋ผ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
10+
if memo[x][y][dir] != -1:
11+
return memo[x][y][dir]
12+
13+
# ์ข…๋ฃŒ ์กฐ๊ฑด: ๋ชฉํ‘œ์— ๋„๋‹ฌํ•˜๋ฉด ๊ฒฝ๋กœ 1๊ฐœ ์ถ”๊ฐ€
14+
if x == N - 1 and y == N - 1:
15+
return 1
16+
17+
# ๊ฒฝ๋กœ ์ˆ˜
18+
paths = 0
19+
20+
for dir_idx in range(3):
21+
# ํ˜„์žฌ ๋ฐฉํ–ฅ์—์„œ ์ด๋™ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
22+
if (dir == 0 and dir_idx == 1) or (dir == 1 and dir_idx == 0):
23+
continue
24+
25+
newX = x + dx[dir_idx]
26+
newY = y + dy[dir_idx]
27+
28+
# ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ๊ฒ€์‚ฌ
29+
if 0 <= newX < N and 0 <= newY < N and arr[newX][newY] == 0:
30+
if dir_idx == 2: # ๋Œ€๊ฐ์„ 
31+
if arr[newX - 1][newY] == 1 or arr[newX][newY - 1] == 1:
32+
continue
33+
34+
# DFS ํƒ์ƒ‰
35+
paths += dfs(newX, newY, dir_idx)
36+
37+
# ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์— ์ €์žฅ
38+
memo[x][y][dir] = paths
39+
return paths
40+
41+
42+
# ์ž…๋ ฅ
43+
arr = [list(map(int, input().split())) for _ in range(N)]
44+
45+
# ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ์ดˆ๊ธฐํ™”: -1๋กœ ์ฑ„์›€
46+
memo = [[[-1] * 3 for _ in range(N)] for _ in range(N)]
47+
48+
# DFS ์‹œ์ž‘
49+
print(dfs(0, 1, 0))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
1. ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ์ง„ํ–‰
3+
1.1 ๋ฌธ์ œ ์š”๊ฑด์— ๋”ฐ๋ผ ์ตœ์•…์˜ ๊ฒฝ์šฐ๋Š”
4+
500 * 500 * 5(ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ์ข…๋ฅ˜) * 4(90๋„ ํšŒ์ „ ๊ฒฝ์šฐ์˜ ์ˆ˜) * 4(๋Œ€์นญํ•œ ํ›„ 90๋„ ํšŒ์ „ ๊ฒฝ์šฐ์˜ ์ˆ˜).
5+
N(1์–ต)์ด 1์ดˆ๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด 2์ดˆ๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค๊ณ  ํŒ๋‹จ.
6+
2. ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ์˜ ์ข…๋ฅ˜๋ณ„๋กœ, ํšŒ์ „๊ณผ ๋Œ€์นญ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ ๋ชจ์–‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.
7+
3. ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋ชจ๋“  ์นธ์„ ์ˆœํšŒํ•˜๋ฉฐ ๋ชจ๋“  ๋ชจ์–‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
8+
9+
'''
10+
11+
N, M = map(int, input().split())
12+
13+
arr = [list(map(int, input().split())) for _ in range(N)]
14+
15+
# ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ๋ชจ์–‘, ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋”ํ•ด์•ผ ํ•  dx, dy์ž„.
16+
shape = [[(0, 1), (0, 2), (0, 3)], [(1, 0), (2, 0), (3, 0)],
17+
[(0, 1), (1, 0), (1, 1)],
18+
[(1, 0), (1, 1), (2, 1)], [(0, -1), (1, -1), (1, -2)],
19+
[(1, 0), (1, -1), (2, -1)], [(0, 1), (1, 1), (1, 2)],
20+
[(1, 0), (2, 0), (2, 1)], [(0, 1), (0, 2), (1, 0)],
21+
[(0, 1), (1, 1), (2, 1)], [(0, 1), (0, 2), (-1, 2)],
22+
[(1, 0), (2, 0), (2, -1)], [(0, 1), (0, 2), (1, 2)],
23+
[(1, 0), (2, 0), (0, 1)], [(1, 0), (1, 1), (1, 2)],
24+
[(1, 0), (1, 1), (1, -1)], [(1, 0), (1, 1), (2, 0)],
25+
[(0, -1), (1, 0), (0, 1)], [(0, 1), (-1, 1), (1, 1)]
26+
]
27+
28+
29+
def calc(i, j, tet):
30+
sum = arr[i][j]
31+
for dx, dy in tet:
32+
newX = i + dx
33+
newY = j + dy
34+
#์ข…์ด์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ข…์ด ์นธ์˜ ์ˆ˜ ๋”ํ•˜๊ธฐ
35+
if 0 <= newX < N and 0 <= newY < M:
36+
sum += arr[newX][newY]
37+
else:
38+
return 0
39+
return sum
40+
41+
42+
ans = 0
43+
for i in range(N):
44+
for j in range(M):
45+
for tet in shape:
46+
temp = calc(i, j, tet) # ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ชจ์–‘์˜ ํ•ฉ ๊ณ„์‚ฐ
47+
ans = max(temp, ans)
48+
49+
print(ans)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ์˜ ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅ
5+
tets = [
6+
# 1x4 ๋ง‰๋Œ€
7+
[(0, 1), (0, 2), (0, 3)], [(1, 0), (2, 0), (3, 0)],
8+
# 2x2 ์ •์‚ฌ๊ฐํ˜•
9+
[(0, 1), (1, 0), (1, 1)],
10+
# ใ„น์ž ๋ชจ์–‘
11+
[(1, 0), (1, 1), (2, 1)], [(0, 1), (1, 0), (1, -1)],
12+
# ใ„น์ž ๋Œ€์นญ
13+
[(1, 0), (1, -1), (2, -1)], [(0, 1), (1, 1), (1, 2)],
14+
# ใ„ด์ž ๋ชจ์–‘ (ํšŒ์ „ ํฌํ•จ)
15+
[(1, 0), (2, 0), (2, 1)], [(0, 1), (0, 2), (1, 0)],
16+
[(0, 1), (1, 1), (2, 1)], [(0, 1), (0, 2), (-1, 2)],
17+
[(1, 0), (2, 0), (2, -1)], [(0, 1), (0, 2), (1, 2)],
18+
# ใ„ด์ž ๋Œ€์นญ (ํšŒ์ „ ํฌํ•จ)
19+
[(1, 0), (2, 0), (0, 1)], [(1, 0), (1, 1), (1, 2)],
20+
[(1, 0), (1, 1), (1, -1)], [(1, 0), (1, 1), (2, 0)],
21+
# ใ…—์ž ๋ชจ์–‘ (ํšŒ์ „ ํฌํ•จ)
22+
[(0, -1), (0, 1), (1, 0)], [(0, 1), (-1, 1), (1, 1)],
23+
[(0, -1), (0, 1), (-1, 0)], [(1, 0), (0, 1), (-1, 0)],
24+
]
25+
26+
# ์ฃผ์–ด์ง„ ์ขŒํ‘œ์—์„œ ํŠน์ • ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ฅผ ๋ฐฐ์น˜ํ–ˆ์„ ๋•Œ์˜ ํ•ฉ ๊ณ„์‚ฐ
27+
def calc(i, j, tet):
28+
total = arr[i][j] # ์‹œ์ž‘ ์ขŒํ‘œ์˜ ๊ฐ’
29+
for di, dj in tet:
30+
ni, nj = i + di, j + dj
31+
# ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฌดํšจ
32+
if 0 <= ni < N and 0 <= nj < M:
33+
total += arr[ni][nj]
34+
else:
35+
return 0
36+
return total
37+
38+
# ๋ชจ๋“  ์œ„์น˜์—์„œ ๊ฐ€๋Šฅํ•œ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ฅผ ๋ฐฐ์น˜ํ•ด ์ตœ๋Œ€๊ฐ’ ๊ณ„์‚ฐ
39+
def max_tetromino_sum():
40+
max_sum = 0
41+
for i in range(N):
42+
for j in range(M):
43+
for tet in tets:
44+
max_sum = max(max_sum, calc(i, j, tet))
45+
return max_sum
46+
47+
48+
N, M = map(int, input().split())
49+
arr = [list(map(int, input().split())) for _ in range(N)]
50+
51+
print(max_tetromino_sum())
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)