https://school.programmers.co.kr/learn/courses/30/lessons/42898
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์ฒ๋ผ ์ขํ์ ์ฌ ์ ์๋ ์ต์๊ฐ์ ์ ์ฅํ๊ณ ๋น๊ตํ๋ ๋ฐฉ์์ด ํจ์จ์ ์ธ๊ฑธ๊น? ๊ทธ๋ ๊ฒ ํ์ด๋ด์ผ๊ฒ ๋ค.
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ์์์ฐพ๊ธฐ(Level 1) - Java (0) | 2023.09.16 |
---|---|
[Programmers] ๋ชจ์๊ณ ์ฌ - Java (0) | 2023.09.16 |
[Programmers] ์ผ๊ทผ ์ง์ - Java (0) | 2023.08.22 |
[Programmers] ์ต๊ณ ์ ์งํฉ - Java (0) | 2023.08.20 |
[Programmers] ์ ์์ผ๊ฐํ - Java (0) | 2023.08.19 |