Skip to content

Commit 1ba9867

Browse files
committed
[Gold IV] Title: 테트로미노, Time: 624 ms, Memory: 81344 KB -BaekjoonHub
1 parent 605fc4b commit 1ba9867

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Gold IV] 테트로미노 - 14500
2+
3+
[문제 링크](https://www.acmicpc.net/problem/14500)
4+
5+
### 성능 요약
6+
7+
메모리: 81344 KB, 시간: 624 ms
8+
9+
### 분류
10+
11+
구현, 브루트포스 알고리즘
12+
13+
### 제출 일자
14+
15+
2026년 1월 31일 23:33:30
16+
17+
### 문제 설명
18+
19+
<p>폴리오미노란 크기가 1×1인 정사각형을 여러 개 이어서 붙인 도형이며, 다음과 같은 조건을 만족해야 한다.</p>
20+
21+
<ul>
22+
<li>정사각형은 서로 겹치면 안 된다.</li>
23+
<li>도형은 모두 연결되어 있어야 한다.</li>
24+
<li>정사각형의 변끼리 연결되어 있어야 한다. 즉, 꼭짓점과 꼭짓점만 맞닿아 있으면 안 된다.</li>
25+
</ul>
26+
27+
<p>정사각형 4개를 이어 붙인 폴리오미노는 테트로미노라고 하며, 다음과 같은 5가지가 있다.</p>
28+
29+
<p style="text-align:center"><a href="https://commons.wikimedia.org/wiki/File:All_5_free_tetrominoes.svg"><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/14500/1.png" style="height:167px; width:250px"></a></p>
30+
31+
<p>아름이는 크기가 N×M인 종이 위에 테트로미노 하나를 놓으려고 한다. 종이는 1×1 크기의 칸으로 나누어져 있으며, 각각의 칸에는 정수가 하나 쓰여 있다.</p>
32+
33+
<p>테트로미노 하나를 적절히 놓아서 테트로미노가 놓인 칸에 쓰여 있는 수들의 합을 최대로 하는 프로그램을 작성하시오.</p>
34+
35+
<p>테트로미노는 반드시 한 정사각형이 정확히 하나의 칸을 포함하도록 놓아야 하며, 회전이나 대칭을 시켜도 된다.</p>
36+
37+
### 입력
38+
39+
<p>첫째 줄에 종이의 세로 크기 N과 가로 크기 M이 주어진다. (4 ≤ N, M ≤ 500)</p>
40+
41+
<p>둘째 줄부터 N개의 줄에 종이에 쓰여 있는 수가 주어진다. i번째 줄의 j번째 수는 위에서부터 i번째 칸, 왼쪽에서부터 j번째 칸에 쓰여 있는 수이다. 입력으로 주어지는 수는 1,000을 넘지 않는 자연수이다.</p>
42+
43+
### 출력
44+
45+
<p>첫째 줄에 테트로미노가 놓인 칸에 쓰인 수들의 합의 최댓값을 출력한다.</p>
46+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Foundation
2+
3+
let input = readLine()!.split(separator: " ").map { Int($0)! }
4+
let N = input[0]
5+
let M = input[1]
6+
7+
var board = Array(repeating: Array(repeating: 0, count: M), count: N)
8+
for i in 0..<N {
9+
board[i] = readLine()!.split(separator: " ").map { Int($0)! }
10+
}
11+
12+
let dx = [1, -1, 0, 0]
13+
let dy = [0, 0, 1, -1]
14+
15+
var visited = Array(repeating: Array(repeating: false, count: M), count: N)
16+
var answer = 0
17+
18+
func dfs(_ x: Int, _ y: Int, _ depth: Int, _ sum: Int) {
19+
if depth == 4 {
20+
answer = max(answer, sum)
21+
return
22+
}
23+
24+
for d in 0..<4 {
25+
let nx = x + dx[d]
26+
let ny = y + dy[d]
27+
28+
if nx < 0 || ny < 0 || nx >= N || ny >= M { continue }
29+
if visited[nx][ny] { continue }
30+
31+
visited[nx][ny] = true
32+
dfs(nx, ny, depth + 1, sum + board[nx][ny])
33+
visited[nx][ny] = false
34+
}
35+
}
36+
37+
func checkT(_ x: Int, _ y: Int) {
38+
let center = board[x][y]
39+
var wings = [Int]()
40+
41+
for d in 0..<4 {
42+
let nx = x + dx[d]
43+
let ny = y + dy[d]
44+
if nx < 0 || ny < 0 || nx >= N || ny >= M { continue }
45+
wings.append(board[nx][ny])
46+
}
47+
48+
if wings.count < 3 { return }
49+
wings.sort(by: >)
50+
answer = max(answer, center + wings[0] + wings[1] + wings[2])
51+
}
52+
53+
for i in 0..<N {
54+
for j in 0..<M {
55+
visited[i][j] = true
56+
dfs(i, j, 1, board[i][j])
57+
visited[i][j] = false
58+
checkT(i, j)
59+
}
60+
}
61+
62+
print(answer)

0 commit comments

Comments
 (0)