알고리즘

백준 24392번 영재의 징검다리 (JAVA)

박카스마시며코딩 2023. 3. 24. 22:34

https://www.acmicpc.net/problem/24392

 

24392번: 영재의 징검다리

첫 줄에 N과 M(1 ≤ N, M ≤ 1,000)이 공백으로 구분되어 주어지고, 그 뒤에는 N줄에 걸쳐 다리의 정보가 주어진다. 강화유리의 경우 1, 일반 유리의 경우 0으로 주어진다.

www.acmicpc.net

 

 

package BOJ.dp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_24392 {
    private static final int LIMIT = 1_000_000_007;
    private static final int BLOCK = 0;
    private static final int NOT_VALID = -1;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        int m = stoi.apply(st.nextToken());
        int[][] map = new int[n][m];
        int[][] dp = new int[n][m];
        for(int i = 0 ; i < n ; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0 ; j < m ; j++){
                map[i][j] = stoi.apply(st.nextToken());
                dp[i][j] = NOT_VALID;
            }
        }
        int result = 0;
        for(int i = 0 ; i < m ; i++){
            if(map[0][i] == BLOCK){
                continue;
            }
            result += cal(0,i,n,m,map,dp);
            result %= LIMIT;
        }
        System.out.println(result);
    }

    private static int cal(int y, int x, int n, int m, int[][] map, int[][] dp) {
        if(y == n-1){
            return 1;
        }
        if(dp[y][x] != NOT_VALID){
            return dp[y][x];
        }
        int result = 0;
        for(int i = -1 ; i <= 1 ; i++){
            int ny = y +1;
            int nx = x + i;
            if(nx < 0 || nx >= m || map[ny][nx] == BLOCK){
                continue;
            }
            result += cal(ny,nx,n,m,map,dp);
            result %= LIMIT;
        }
        dp[y][x] = result;
        return result;
    }
}