[백준] 2210 숫자판 점프 / DFS (C++)

sobokii·2023년 9월 27일
0

문제풀이

목록 보기
1/52


DFS 활용 문제
중복 원소 제거 작업을 거치지 않으려고 set 자료구조를 사용하였다.

#include
#include
#include
using namespace std;

int dx[4] = { 0, 0, -1 , 1 };
int dy[4] = { -1, 1, 0, 0 };

vector<vector<int>> jump(5, vector<int>(5));
set<int> ans;

void DFS(int x, int y, int num, int cnt)
{
    if (cnt == 6)
    {
        ans.insert(num);
        return;
    }
    else
    {
        for (int i = 0; i < 4; i++)
        {
            // 여기서 실제 x, y 값을 변화시키는 실수가 있었다.
            int nx = x + dx[i];
            int ny = y + dy[i];

            if (nx < 0 || ny < 0 || nx > 4 || ny > 4)
            {
                continue;
            }

            DFS(nx, ny, num * 10 + jump[nx][ny], cnt + 1);
        }
    }
}


int main()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            cin >> jump[i][j];
        }
    }

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            DFS(i, j, 0, 0);
        }
    }

    cout << ans.size();
}
profile
직장 구하고 있습니다.

0개의 댓글