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์ฒ๋ผ ์ขํ์ ์ฌ ์ ์๋ ์ต์๊ฐ์ ์ ์ฅํ๊ณ ๋น๊ตํ๋ ๋ฐฉ์์ด ํจ์จ์ ์ธ๊ฑธ๊น? ๊ทธ๋ ๊ฒ ํ์ด๋ด์ผ๊ฒ ๋ค.