Home [프로그래머스] 등굣길
Post
Cancel

[프로그래머스] 등굣길

문제 주소

  • https://programmers.co.kr/learn/courses/30/lessons/42898
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>

using namespace std;

// D[i][j] = D[i-1][j] + D[i][j-1];
// puddles 행렬이 반대
int solution(int m, int n, vector<vector<int>> puddles) {
    int answer = 0;
    int map[101][101] = {0, };
    for (const auto &p : puddles) {
        map[p[1]][p[0]] = -1;
    }

    map[1][1] = 1;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (i == 1 && j == 1) continue;
            if (map[i][j] == -1) continue;

            map[i][j] += (map[i-1][j] == -1)? 0 : map[i-1][j];
            map[i][j] %= 1000000007;
            map[i][j] += (map[i][j-1] == -1)? 0 : map[i][j-1];
            map[i][j] %= 1000000007;
        }
    }

    return map[n][m];
}

아이디어

  • 오른쪽과 아래쪽으로만 움직일 수 있다.
  • 집 좌표 : (1, 1), 학교 좌표 : (n, m)
  • 학교 좌표에서 왼쪽과 위쪽만 확인할 수 있다.
  • 점화식 : D[i][j] = D[i-1][j] + D[i][j-1]
    • 즉, D[n][m] = D[n-1][m] + D[n][m-1]

image

주의할 점

  • puddles의 좌표와 m, n의 좌표가 반대로 되어있음
This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 단속카메라

[프로그래머스] 도둑질

Comments powered by Disqus.