반응형
출처: https://www.acmicpc.net/problem/2529
Approach
DFS (depth-first search, 깊이 우선 탐색)
깊이 우선 탐색(depth-first search, DFS)
DFS 그래프의 모든 정점을 방문할 때 최대한 깊숙히 정점을 방문하는 방식입니다. - Stack 이용 즉, 연결된 정점을 따라 계속 순회하다가 갈 곳이 없어지며 바로 이전단계로 가는 백트래킹 방식.
zoosso.tistory.com
DFS와 BFS 비교
출처: 나무위키 경로 비교 BFS - Queue 이용 - 최소신장트리(Minimum Spanning Trees), 최단경로(Shortest Paths) 장점: 무한히 깊거나 무한에 가까운 트리인 경우에 효과적 단점: 목표 노드로 가는 경로들 모..
zoosso.tistory.com
#include <stdio.h>
typedef long long LL;
LL ansMax, ansMin = (LL)99e8;
int N, info[11], permut[11];
int used[11];
void DFS(int lev) {
if (lev > N) {
LL d = 0;
for (int i = 0; i <= N; ++i)
d = d * 10 + permut[i];
if (d > ansMax) ansMax = d;
if (d < ansMin) ansMin = d;
return;
}
for (int i = 0; i < 10; ++i) if (!used[i]) {
permut[lev] = i;
if (lev) {
if (info[lev] && permut[lev - 1] > permut[lev]) continue;
if (!info[lev] && permut[lev - 1] < permut[lev]) return;
}
used[i] = 1;
DFS(lev + 1);
used[i] = 0;
}
}
int main() {
// freopen("input.txt", "r", stdin);
scanf("%d", &N);
char cmd;
for (int i = 1; i <= N; ++i) {
scanf(" %c", &cmd);
info[i] = cmd == '<';
}
DFS(0);
printf("%0*lld\n", N + 1, ansMax);
printf("%0*lld\n", N + 1, ansMin);
}

반응형
'PS 문제 풀이 > Baekjoon' 카테고리의 다른 글
[BOJ] 백준 2660 회장뽑기 (0) | 2021.04.03 |
---|---|
[BOJ] 백준 2463 비용 (0) | 2021.04.03 |
[BOJ] 백준 2665 미로만들기 (0) | 2021.03.31 |
[BOJ] 백준 14867 물통 (0) | 2021.03.30 |
[BOJ] 백준 17616 등수 찾기 (0) | 2021.03.13 |
댓글