잠토의 잠망경

[알고리즘] 순열 본문

공부/알고리즘

[알고리즘] 순열

잠수함토끼 2021. 5. 30. 10:26

조합

요소 : 1, 2, 3, 4 의 조합가능한 경우의 수를 만들어낸다.


int visit[MAXS];
int sample[MAXS];

// now는 sample에 저장될 위치이다.
void dfs(int now)
{
    if (now >= S)
    {
        for (int i = 1; i <= S; i++)
            printf("%d ", sample[i]);

        printf("\n");

        // 필요하다면 여기서 연산하면 된다.
        // 조합된 결과를 여기서 
    }

    for (int i = 1; i <= S; i++)
    {
        // 중복으로 걸리는걸 막는다.
        if (visit[i] != 0) 
            continue;

        sample[now] = shipSize[i];

        visit[i] = 1;

        dfs(now + 1);

        visit[i] = 0;

    }

}

dfs(0);
Comments