๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] 1520. ๋‚ด๋ฆฌ๋ง‰๊ธธ

by Seongpyo Hong 2021. 4. 1.

๋ฌธ์ œ

 

ํ’€์ด

๋จผ์ € ๋ชจ๋“  ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— DFS๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค.

์ƒ๊ฐํ•œ ์•„์ด๋””์–ด๋Š” ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ขŒํ‘œ๋ฅผ x,y๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, (x,y) ์—์„œ (N-1, N-1)๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋Š” ์ด๋ฏธ ๊ตฌํ•ด์ ธ์žˆ๊ณ  ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ dfs๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•˜๊ณ , ๋ฐฉ๋ฌธ์ฒดํฌ๊ฐ€ ๋œ ๊ณณ์ธ ๊ฒฝ์šฐ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ฃผ์˜ํ•  ์ ์ด ์žˆ๋‹ค. ์ดˆ๊ธฐ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

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

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int M, N, total = 0;
    static int[][] map;
    static int[][] cache;
    static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    public static void main(String[] args) throws IOException {
        String[] input = br.readLine().split(" ");
        M = Integer.parseInt(input[0]);
        N = Integer.parseInt(input[1]);
        map = new int[M][N];
        cache = new int[M][N];

        for (int i = 0; i < M; i++) {
            String[] line = br.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(line[j]);
            }
        }
        
        System.out.println(dfs(0, 0));
    }

    private static int dfs(int x, int y) {
        if (x == M - 1 && y == N - 1) {
            return 1;
        }

        if (cache[x][y] > 0)
            return cache[x][y];

        for (int i = 0; i < 4; i++) {
            int nX = x + dx[i];
            int nY = y + dy[i];
            if (nX < 0 || nY < 0 || nX >= M || nY >= N || map[x][y] <= map[nX][nY]) continue;
            cache[x][y] += dfs(nX, nY);
        }

        return cache[x][y];
    }
}

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด์œ ๋Š” cache[x][y]๊ฐ€ 0๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์บ์‹ฑ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋–„๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, 0์ธ ๊ฒฝ์šฐ, ํ—ค๋‹น ์ขŒํ‘œ๋ฅผ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ์ง€๋งŒ ์œ ๋งํ•˜์ง€ ์•Š์€์ง€ ์•„๋‹ˆ๋ฉด ์•„์ง ๋ฐฉ๋ฌธ์„ ํ•˜์ง€ ์•Š์€ ์ง€์ ์ธ์ง€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ์ค‘๋ณต ํƒ์ƒ‰์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋–„๋ฌธ์ด๋‹ค. ์ด ๋ถ€๋ถ„์„ ๋ฐฉ๋ฌธ์ฒดํฌ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ตœ์ข… ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

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

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int M, N, total = 0;
    static int[][] map;
    static int[][] cache;
    static boolean[][] visited;
    static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    public static void main(String[] args) throws IOException {
        String[] input = br.readLine().split(" ");
        M = Integer.parseInt(input[0]);
        N = Integer.parseInt(input[1]);
        map = new int[M][N];
        cache = new int[M][N];
        visited = new boolean[M][N];
        for (int i = 0; i < M; i++) {
            String[] line = br.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(line[j]);
            }
        }

        System.out.println(dfs(0, 0));
    }

    private static int dfs(int x, int y) {
        if (x == M - 1 && y == N - 1) {
            return 1;
        }

        if (visited[x][y])
            return cache[x][y];

        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nX = x + dx[i];
            int nY = y + dy[i];
            if (nX < 0 || nY < 0 || nX >= M || nY >= N || map[x][y] <= map[nX][nY]) continue;
            cache[x][y] += dfs(nX, nY);
        }

        return cache[x][y];
    }
}

๋Œ“๊ธ€