Algorithm/DP

BOJ#1937 욕심쟁이 판다

밤이2209 2017. 4. 12. 16:19

BOJ#1937 욕심쟁이 판다


* 문제


* 풀이

dp + 탐색


* 나의 코드

https://github.com/stack07142/BOJ/blob/9fb5631acdf38f48b28e2ee02bc0ac4a6a4c3a7f/BOJ%231937_GreedyPanda/src/Main.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
* BOJ#1937 욕심쟁이 판다
* https://www.acmicpc.net/problem/1937
*/

public class Main {

static int n;
static int[][] dp = new int[501][501];
static int[][] map = new int[501][501];

static final int[] dRow = {0, -1, 0, 1};
static final int[] dCol = {-1, 0, 1, 0};

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

n = Integer.parseInt(br.readLine());

for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {

map[i][j] = Integer.parseInt(st.nextToken());
}
}

int maxValue = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {

maxValue = Math.max(maxValue, solve(i,j));
}
}

System.out.println(maxValue);
}

static int solve(int row, int col) {

if (dp[row][col] > 0) return dp[row][col];

dp[row][col] = 1;
for (int i = 0; i < 4; i++) {

int nextRow = row + dRow[i];
int nextCol = col + dCol[i];

if (nextRow < 0 || nextRow >= n || nextCol < 0 || nextCol >= n) continue;
if (map[nextRow][nextCol] <= map[row][col]) continue;

dp[row][col] = Math.max(dp[row][col], solve(nextRow, nextCol) + 1);
}

return dp[row][col];
}
}


'Algorithm > DP' 카테고리의 다른 글

BOJ#9084 동전  (0) 2017.05.01
BOJ#14501 퇴사  (0) 2017.04.22
BOJ#2666 벽장문의 이동  (0) 2017.04.08
BOJ#5557 1학년  (0) 2017.04.08
BOJ#2098 외판원 순회  (0) 2017.04.07