본문 바로가기
전공공부/코딩테스트

(c++) 프로그래머스 "땅따먹기"

by 시아나 2022. 4. 16.

https://programmers.co.kr/learn/courses/30/lessons/12913#

 

코딩테스트 연습 - 땅따먹기

땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟

programmers.co.kr


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<vector<int> > land)
{
	for (int i = 1; i < land.size(); i++) {
		land[i][0] += max(max(land[i-1][1],land[i-1][2] ), land[i - 1][3]);
		land[i][1] += max(max(land[i - 1][0], land[i - 1][2]), land[i - 1][3]);
		land[i][2] += max(max(land[i - 1][0], land[i - 1][1]), land[i - 1][3]);
		land[i][3] += max(max(land[i - 1][0], land[i - 1][1]), land[i - 1][2]);
	}
	return *max_element(land[land.size() - 1].begin(), land[land.size() - 1].end());
}

 

DP는 코딩테스트에서 처음 써봐서 생각하는데 조금 걸렸다.