Skip to content

Commit a41e29c

Browse files
committed
[BOJ] #14500. 테트로미노 / 골드4 / 60분 / 실패
1 parent 3358a3a commit a41e29c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
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)