๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/42898

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


1. ๋ฌธ์ œ

mxn ํฌ๊ธฐ map์—์„œ ๋ฌผ์›…๋ฉ์ด๋ฅผ ์ œ์™ธํ•œ ์ขŒํ‘œ๋ฅผ ์ง€๋‚˜ 1,1์—์„œ m,n๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜๋ฅผ 1,000,000,007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•œ๋‹ค.

 

2. ํ’€์ด

์™„์ „ํƒ์ƒ‰์œผ๋กœ ์ ‘๊ทผํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ ์‹คํŒจ+์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

dp๋กœ ํ’€์–ด์•ผํ•  ๋“ฏ ํ•˜๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ์ฒ˜๋Ÿผ ํ’€์–ด๋ณด์ž.

 

 

3. ์ฝ”๋“œ

์™„ํƒ ์‹คํŒจ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
class Solution {
    
    static long min = Integer.MAX_VALUE;
    static int M, N, map[][];
    static int[] dy={0,1};
    static int[] dx={1,0};
    
    public int solution(int m, int n, int[][] puddles) {
        M = m;
        N = n;
        map = new int[m+1][n+1];
        for (int[] loc: puddles) {
            map[loc[0]][loc[1]] = 1;
        }
        dfs(1,1,0);
        
        return (int) (min%1000000007);
    }
    
    static void dfs(int y, int x, int cnt) {
        if (cnt > min) return;
        if (y==M && x==N) {
            if (cnt-1 < min) min = cnt-1;
            return;
        }
        
        for (int d=0; d<2; d++) {
            int ny = y+dy[d];
            int nx = x+dx[d];
            if (ny<=0 || ny>M || nx<=0 || nx>N || map[ny][nx] == 1) continue;
            dfs(ny, nx, cnt+1);
        }
    }
}

 

 

4. ์‚ฌ๋‹ด

[23/8/4]

์™„ํƒ์œผ๋กœ ํ’€์—ˆ๋Š”๋ฐ ์‹คํŒจ + ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค. dp์ฒ˜๋Ÿผ ์ขŒํ‘œ์— ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Ÿ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ด ํšจ์œจ์ ์ธ๊ฑธ๊นŒ? ๊ทธ๋ ‡๊ฒŒ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.