문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12913
코딩테스트 연습 - 땅따먹기
땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟
programmers.co.kr
쉬운 DP 문제였습니다.
cache[y][x] = land[y][x]의 지점부터 얻을 수 있는 최대 점수라는 점만 기억하면 쉽게 접근할 수 있습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <cstring> | |
#include <iostream> | |
using namespace std; | |
const int COL_MAX = 1e5; | |
const int ROW_MAX = 4; | |
int cache[COL_MAX][ROW_MAX]; | |
int getMaxScore(int y, int x, vector<vector<int>> &land) | |
{ | |
if (y == land.size()) | |
{ | |
return 0; | |
} | |
int &result = cache[y][x]; | |
if (result != -1) | |
{ | |
return result; | |
} | |
result = land[y][x]; | |
int temp = 0; | |
for (int nextX = 0; nextX < ROW_MAX; nextX++) | |
{ | |
if (nextX == x) | |
{ | |
continue; | |
} | |
temp = max(temp, getMaxScore(y + 1, nextX, land)); | |
} | |
result += temp; | |
return result; | |
} | |
int solution(vector<vector<int> > land) | |
{ | |
memset(cache, -1, sizeof(cache)); | |
int answer = 0; | |
for (int i = 0; i < ROW_MAX; i++) | |
{ | |
answer = max(answer, getMaxScore(0, i, land)); | |
} | |
return answer; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 최댓값과 최솟값 (0) | 2022.03.05 |
---|---|
[Programmers] 숫자의 표현 (0) | 2022.03.05 |
[Programmers] 다음 큰 숫자 (0) | 2022.03.04 |
[Programmers] [3차] n진수 게임 (0) | 2022.03.04 |
[Programmers] 올바른 괄호 (0) | 2022.03.01 |