반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 익명클래스
- 재귀함수
- 삼성sw문제
- 알고리즘
- 멀티스레드
- 자바
- 삼성SW테스트
- Android
- CKLU
- 모바일
- 개발
- 익명객체
- 백준 알고리즘
- 백준
- Java
- 금융IT
- dfs
- backjoon
- 프로그래머스
- 다이나믹 프로그래밍
- IT
- 안드로이드
- 언더라이터
- 현대오토에버 코딩테스트
- dp
- BFS
- 조합
- 네트워크
- 너비탐색
- 데이터베이스
Archives
- Today
- Total
Limky 삽질블로그
[DP] 땅따먹기 게임 (Hopscotch) 본문
반응형
Programmers Level_03
땅 따먹기 게임 (Hopscotch)
영희는 땅따먹기 게임에 푹 빠졌습니다. 땅따먹기 게임의 땅은 총 N행 4열로 나누어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 땅을 밟으면서 한 행씩 내려올 때, 영희는 각 행의 4칸 중 1칸만 밟으면서 내려올 수 있습니다. 땅따먹기 게임에는 같은 열을 연속해서 밟을 수가 없는 특수 규칙이 있습니다. 즉, 1행에서 (5)를 밟았다면, 2행의 (8)은 밟을 수가 없게 됩니다. 마지막 행까지 모두 내려왔을 때, 점수가 가장 높은 사람이 게임의 승자가 됩니다. 여러분이 hopscotch 함수를 제작하여 영희가 최대 몇 점을 얻을 수 있는지 알려주세요. 예를 들어
1 2 3 5
5 6 7 8
4 3 2 1
의 땅이 있다면, 영희는 각 줄에서 (5), (7), (4) 땅을 밟아 16점을 최고점으로 받을 수 있으며, hopscotch 함수에서는 16을 반환해주면 됩니다.
생각보다 어려워서..다른 분 해설을 참고해서 dp로 풀었습니다...dp는 아직 연습이 더 필요한 것 같습니다.
package Programmers.level04;
class Hopscotch {
public static void main(String[] args) {
Hopscotch c = new Hopscotch();
int[][] test = { { 1, 2, 3, 5 }, { 5, 6, 7, 8 }, { 4, 3, 2, 1 } };
//아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println(c.hopscotch(test, 3));
}
int hopscotch(int[][] board, int size) {
int result = 0;
int [][] dp = new int[size][board[0].length];
for(int i=0; i<4; i++) dp[0][i] = board[0][i];
int ans=0;
for(int i=1; i<size; i++)
for(int j=0; j<4 ; j++)
for(int k=0; k<4; k++)
if(j!=k){
dp[i][j] = Math.max(dp[i][j], board[i][j] + dp[i-1][k]);
}
//printArray(dp);
for(int i=0; i<4; i++)
result = Math.max(result, dp[size-1][i]);
return result;
}
private static void printArray(int[][] ground) {
for(int i=0; i<ground.length; i++){
for(int j=0; j<ground[0].length; j++){
System.out.print(ground[i][j]+" ");
}
System.out.println();
}
}
}
그리고... 인상깊었던 다른 분의 코드...직관적입니다...이런 방법도 있군요..
class Hopscotch { int hopscotch(int[][] board, int size) { for(int i=1; i<size; i++) { board[i][0] += Math.max(board[i-1][1], Math.max(board[i-1][2], board[i-1][3])); board[i][1] += Math.max(board[i-1][0], Math.max(board[i-1][2], board[i-1][3])); board[i][2] += Math.max(board[i-1][0], Math.max(board[i-1][1], board[i-1][3])); board[i][3] += Math.max(board[i-1][0], Math.max(board[i-1][1], board[i-1][2])); } return Math.max(board[size-1][0], Math.max(board[size-1][1], Math.max(board[size-1][2], board[size-1][3]))); } public static void main(String[] args) { Hopscotch c = new Hopscotch(); int[][] test = { { 1, 2, 3, 5 }, { 5, 6, 7, 8 }, { 4, 3, 2, 1 } }; //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(c.hopscotch(test, 3)); } }
반응형
'Algorithm > Programmers' 카테고리의 다른 글
| [프로그래머스] #단어변환(DFS) (0) | 2019.04.01 |
|---|---|
| [DP] 가장 큰 정사각형 찾기 (find Largest Square) (3) | 2017.11.10 |
| [Algorithm] 야근지수 (NoOverTime) (0) | 2017.11.08 |
| [Algorithm] 최고의 집합 (Best Set) (0) | 2017.11.08 |
| [Algorithm] 점프뛰기 (JumpCase) (0) | 2017.10.05 |