[백준 바킹독] 0x09 - BFS 토마토(7576)
Introduction
백트래킹은 구하고자 하는 해를 튜플로 나타내고 튜플에 기준 함수(한정 함수)를 적용했을 때의 결과가 최대치, 최소치 혹은 일정 조건을 만족하게끔 만들어주는 퇴각 검색 기법으로 정의된다. 백트래킹은 해답이 될 수 있는 튜플을 완성해 나아가며 그 과정에서 미완성된 튜플에 한정 함수를 적용하여 해답의 가능성이 없는 튜플들은 더이상 진행시키지 않는 방법을 사용한다. 이때 튜플을 만드는 과정에서 스택(Stack)을 사용하여 한정 함수를 만족하면 push, 만족하지 않으면 pop을 하는 방법을 사용한다. 이런 과정이 퇴각과 전진하는 것과 비슷하다 하여 퇴각 검색 기법(백트래킹, backtracking)이라 불린다.
나올 수 있는 모든 조합의 수를 DFS를 이용한 전체 탐색을 통해 확인
이때 명시적 제약조건은 다음과 같다.
명시적 제약 조건 : 트리의 각 노드는 0 또는 1의 값을 가진다. 이때 i번째 높이의 노드는 i번째 원소의 선택 여부를 나타내며 0은 선택하지 않음, 1은 선택함을 나타낸다.
또한 i번째 높이의 노드는 문제 상황에서 만들어질 수 있는 집합 S 원소이다.
루트 부터 DFS를 하여 불필요한 탐색을 하지 않고 한정 함수를 만족하는 (x[1], x[2], …, x[n])를 찾는 것을 목표로 한다.
각 노드 s 에 도착할 때마다 루트에서 s까지의 경로 예를 들어 k번째 선택에선 (x[1], x[2], …, x[k])의 튜플이 한정함수를 만족하는지 판단한다.
k < n 일때 x[k]의 자식 노드들인 x[k+1] 들 즉, T(x[1], … x[k])을 탐색한다.
모든 자식 노드를 탐색했다면 현재 노드를 dead node로 변경한다.
진행 도중 한정함수를 만족하는 (x[1], x[2], …, x[n])에 도달하면 정답을 출력한다.
재귀
backtrack(int k){
/*
T(x[1], ... x[k-1]) : (x[1], ..., x[k])의 경로가 문제 상태에 속하도록 하는 모든 x[k]의 집합
의 모든 원소에 대해 반복
*/
for each x[k] in T(x[1], ..., x[k-1]){
/*
(x[1], x[2], ..., x[k])의 경로에 대하여 한정 함수를 만족하면 x[k]를 live node이자 E-node로 만듬
*/
if(B_k(x[1], x[2], ..., x[k]) == true){
/*
(x[1], x[2], ..., x[k])가 한정함수를 만족하고 정답 노드라면 결과를 생성
*/
if(x[1], x[2], ... x[k] 가 정답 노드라면 결과 out)
/*
k가 n보다 작으면 x[k]의 자식 노드들의 활성화를 위해 backtrack(k+1) 호출
*/
if(k<n) backtrack(k+1);
/*
backtrack(k+1)를 통해 x[k]의 모든 자식 노드가 생성되었으므로
x[k]를 dead node로 만듬
*/
}
}
}
반복문
ibacktrack(int n){
int k = 1;
while(k != 0){
/*
T(x[1], ... x[k-1]) : (x[1], ..., x[k])의 경로가 문제 상태에 속하도록 하는 모든 x[k]의 집합
의 시도하지 않은 x[k]에 대해 (x[1], x[2], ..., x[k])가 한정함수를 만족하면 x[k]를 live node이자 E-node로 만듬
이때 T(x[1], ... x[n])은 공집합이므로 바로 else로 진입함
*/
if(there remains an untried x[k] in T(x[1], x[2], ... , x[k-1])
and B_k(x[1], ... , x[k]) == true){
if(x[1], ..., x[k] 가 정답 노드라면 결과 out)
/*
x[k]의 자식 노드들의 활성화를 위해 k를 증가
*/
k = k + 1;
}else{
/*
더 이상 확인할 자식이 없거나 한정함수를 만족하는 자식이 없다면 x[k]를 dead node로 만듬
*/
k = k - 1;
}
}
}
ref. https://chanhuiseok.github.io/posts/algo-23/
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
1167 트리의 지름
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Git 명령어를 사용한 하위 디렉토리 다운로드 Clone 할 로컬 저장소 생성
Introduction
# Fetch the submodule commits into the main repository git remote add submodule_origin git://url/to/submodule/origin git fetch submodule_origin
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Graph
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Spring Project
the page for java
가벼운 Base image를 사용
version: 3.0.0a10
WIDTH
version: 3.0.0a10
#include <iostream> #include <thread> #include <chrono> #include <mutex> #include <atomic> #include <string.h>
version: 3.0.0a10
https://cplusplus.com/reference/future/
Multithreading support was introduced in C++11.
how to costom github blog using jekyll
You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different wa...