관리 메뉴

DuckingRacoon

프로그래머스 | [DP] 등굣길 본문

알고리즘

프로그래머스 | [DP] 등굣길

따킹라쿤

출처

🌐 코딩테스트 연습 - 등굣길 | 프로그래머스 스쿨

문제

문제 설명

계속되는 폭우로 일부 지역이 물에 잠겼습니다. 물에 잠기지 않은 지역을 통해 학교를 가려고 합니다. 집에서 학교까지 가는 길은 m x n 크기의 격자모양으로 나타낼 수 있습니다.

아래 그림은 m = 4, n = 3 인 경우입니다.

가장 왼쪽 위, 즉 집이 있는 곳의 좌표는 (1, 1)로 나타내고 가장 오른쪽 아래, 즉 학교가 있는 곳의 좌표는 (m, n)으로 나타냅니다.

격자의 크기 m, n과 물이 잠긴 지역의 좌표를 담은 2차원 배열 puddles이 매개변수로 주어집니다. 오른쪽과 아래쪽으로만 움직여 집에서 학교까지 갈 수 있는 최단경로의 개수를 1,000,000,007로 나눈 나머지를 return 하도록 solution 함수를 작성해주세요.

제한사항

  • 격자의 크기 m, n은 1 이상 100 이하인 자연수입니다.
    • m과 n이 모두 1인 경우는 입력으로 주어지지 않습니다.
  • 물에 잠긴 지역은 0개 이상 10개 이하입니다.
  • 집과 학교가 물에 잠긴 경우는 입력으로 주어지지 않습니다.

입출력 예시

m n puddles return

4 3 [[2, 2]] 4

풀이

1차 풀이 (실패)

#include <string>
#include <vector>

using namespace std;
const int MAXCOUNT = 1000000007;

int solution(int m, int n, vector<vector<int>> puddles) 
{
    vector<vector<pair<int, int>>> countMap(n, vector<pair<int, int>>(m, { 0, 0 }));

    for (const vector<int>& pud : puddles)
    {
        countMap[pud[1]-1][pud[0]-1] = { -1, -1 };
    }

    countMap[0][0] = { 0, 1};
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (countMap[i][j].first == -1)
                continue;

            if (i != 0 && countMap[i - 1][j].first != -1)
            {
                pair<int, int> left = countMap[i-1][j];
                left.first += 1;

                countMap[i][j] = left;
            }

            if (j != 0 && countMap[i][j - 1].first != -1)
            {
                pair<int, int> up = countMap[i][j - 1];
                up.first += 1;

                if(countMap[i][j] == pair<int,int>(0,0) || countMap[i][j].first > up.first)
                {
                    countMap[i][j] = up;
                }
                else if(countMap[i][j].first == up.first)
                {
                    countMap[i][j].second += up.second;
                }
            }
        }
    }
    return countMap[n-1][m-1].second % MAXCOUNT;
}

2차 (일부 실패)

#include <string>
#include <vector>

using namespace std;
const int MAXCOUNT = 1000000007;

int solution(int m, int n, vector<vector<int>> puddles) 
{
    vector<vector<pair<int, int>>> countMap(n, vector<pair<int, int>>(m, { 0, 0 }));

    for (const vector<int>& pud : puddles)
    {
        countMap[pud[1]-1][pud[0]-1] = { -1, -1 };
    }

    countMap[0][0] = { 0, 1};
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (countMap[i][j].first == -1)
                continue;

            if (i != 0 && countMap[i - 1][j].first != -1)
            {
                pair<int, int> left = countMap[i-1][j];
                left.first += 1;
                left.second %= MAXCOUNT;

                countMap[i][j] = left;
            }

            if (j != 0 && countMap[i][j - 1].first != -1)
            {
                pair<int, int> up = countMap[i][j - 1];
                up.first += 1;
                up.second %= MAXCOUNT;

                if(countMap[i][j] == pair<int,int>(0,0) || countMap[i][j].first > up.first)
                {
                    countMap[i][j] = up;
                }
                else if(countMap[i][j].first == up.first)
                {
                    countMap[i][j].second += up.second;
                    countMap[i][j].second %= MAXCOUNT;
                }
            }
        }
    }
    return countMap[n-1][m-1].second % MAXCOUNT;
}

3차

#include <string>
#include <vector>

using namespace std;
const int MAXCOUNT = 1000000007;

int solution(int m, int n, vector<vector<int>> puddles) 
{
    vector<vector<pair<int, int>>> countMap(n, vector<pair<int, int>>(m, { 0, 0 }));

    for (const vector<int>& pud : puddles)
    {
        countMap[pud[1]-1][pud[0]-1] = { -1, -1 };
    }

    countMap[0][0] = { 0, 1};
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (countMap[i][j].first == -1)
                continue;

            if (i != 0 && countMap[i - 1][j].second > 0)
            {
                pair<int, int> left = countMap[i-1][j];
                left.first += 1;

                countMap[i][j] = left;
            }

            if (j != 0 && countMap[i][j - 1].second > 0)
            {
                pair<int, int> up = countMap[i][j - 1];
                up.first += 1;

                if(countMap[i][j] == pair<int,int>(0,0) || countMap[i][j].first > up.first)
                {
                    countMap[i][j] = up;
                }
                else if(countMap[i][j].first == up.first)
                {
                    countMap[i][j].second += up.second;
                }
            }

            countMap[i][j].second %= MAXCOUNT;

        }
    }
    return countMap[n-1][m-1].second % MAXCOUNT;
}