Skip to content

Commit adf8ee7

Browse files
committed
[level 2] Title: 땅따먹기, Time: 13.34 ms, Memory: 31.2 MB -BaekjoonHub
1 parent 2ec3499 commit adf8ee7

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [level 2] 땅따먹기 - 12913
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12913)
4+
5+
### 성능 요약
6+
7+
메모리: 31.2 MB, 시간: 13.34 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 59.8<br/>효율성: 40.2<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 03월 30일 16:37:29
20+
21+
### 문제 설명
22+
23+
<p>땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟으면서 내려와야 합니다. <strong>단, 땅따먹기 게임에는 한 행씩 내려올 때, 같은 열을 연속해서 밟을 수 없는 특수 규칙이 있습니다.</strong> </p>
24+
25+
<p>예를 들면, </p>
26+
27+
<p>| 1 | 2 | 3 | 5 |</p>
28+
29+
<p>| 5 | 6 | 7 | 8 |</p>
30+
31+
<p>| 4 | 3 | 2 | 1 |</p>
32+
33+
<p>로 땅이 주어졌다면, 1행에서 네번째 칸 (5)를 밟았으면, 2행의 네번째 칸 (8)은 밟을 수 없습니다. </p>
34+
35+
<p>마지막 행까지 모두 내려왔을 때, 얻을 수 있는 점수의 최대값을 return하는 solution 함수를 완성해 주세요. 위 예의 경우, 1행의 네번째 칸 (5), 2행의 세번째 칸 (7), 3행의 첫번째 칸 (4) 땅을 밟아 16점이 최고점이 되므로 16을 return 하면 됩니다.</p>
36+
37+
<h5>제한사항</h5>
38+
39+
<ul>
40+
<li>행의 개수 N : 100,000 이하의 자연수</li>
41+
<li>열의 개수는 4개이고, 땅(land)은 2차원 배열로 주어집니다.</li>
42+
<li>점수 : 100 이하의 자연수</li>
43+
</ul>
44+
45+
<h5>입출력 예</h5>
46+
<table class="table">
47+
<thead><tr>
48+
<th>land</th>
49+
<th>answer</th>
50+
</tr>
51+
</thead>
52+
<tbody><tr>
53+
<td>[[1,2,3,5],[5,6,7,8],[4,3,2,1]]</td>
54+
<td>16</td>
55+
</tr>
56+
</tbody>
57+
</table>
58+
<h5>입출력 예 설명</h5>
59+
60+
<p>입출력 예 #1<br>
61+
문제의 예시와 같습니다.</p>
62+
63+
64+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
#include <algorithm>
5+
6+
int solution(vector<vector<int> > land)
7+
{
8+
int answer = -1;
9+
10+
int rows = land.size();
11+
int cols = land[0].size();
12+
13+
14+
vector<vector<int>> dp(rows, vector<int>(cols, 0));
15+
16+
for(int i=0; i<rows; i++){
17+
18+
for(int j=0; j<cols; j++){
19+
20+
if(i==0){
21+
dp[i][j] = land[i][j];
22+
}
23+
else{
24+
int maxVal = -1;
25+
for(int k=0; k<cols; k++){
26+
if(k != j){
27+
int tmp = dp[i-1][k] +land[i][j];
28+
maxVal = max(tmp, maxVal);
29+
}
30+
}
31+
dp[i][j] = maxVal;
32+
}
33+
34+
}
35+
36+
37+
}
38+
39+
40+
for(int i=0; i<cols; i++){
41+
answer = max(answer, dp[rows-1][i]);
42+
}
43+
44+
45+
return answer;
46+
}

0 commit comments

Comments
 (0)