본문 바로가기

backtracking

(6)
[BOJ] 7490번 0 만들기 (C++) https://www.acmicpc.net/problem/7490 7490번: 0 만들기 각 테스트 케이스에 대해 ASCII 순서에 따라 결과가 0이 되는 모든 수식을 출력한다. 각 테스트 케이스의 결과는 한 줄을 띄워 구분한다. www.acmicpc.net #include #include #include using namespace std; int n; bool isZero(string s){ vector num; vector op; string tmp; for(int i = 0; i < s.length(); i++){ if(s[i] == '+' || s[i] == '-'){ num.push_back(stoi(tmp)); op.push_back(s[i]); tmp = ""; } else if(s[i] ..
[BOJ] 19942번 다이어트 (C++) https://www.acmicpc.net/problem/19942 19942번: 다이어트 식재료 N개 중에서 몇 개를 선택해서 이들의 영양분(단백질, 탄수화물, 지방, 비타민)이 일정 이상이 되어야 한다. 아래 표에 제시된 6가지의 식재료 중에서 몇 개를 선택해서 이들의 영양분의 각 www.acmicpc.net #include #include using namespace std; struct Ingredient{ int p, f, s, v, c; }; int n, mp, mf, ms, mv; vector v; vector tmp; vector ans; int answer = 987654321; void dfs(int idx, int sp, int sf, int ss, int sv, int sc){ if..
[CodeTree] 바이러스 백신 (C++) [삼성 SW 역량테스트 기출] https://www.codetree.ai/training-field/frequent-problems/problems/vaccine-for-virus 코드트리 | 코딩테스트 준비를 위한 알고리즘 정석 국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요. www.codetree.ai #include #include #include #include #include using namespace std; int n, m, answer = 987654321; int map[50][50]; int tmp[50][50]; int visited[50][50]; vector hospital; vector picked; bool isUsed[10]; ..
[CodeTree] 방화벽 설치하기 (C++) [삼성 SW 역량테스트 기출] https://www.codetree.ai/training-field/frequent-problems/problems/firewall-installation 코드트리 | 코딩테스트 준비를 위한 알고리즘 정석 국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요. www.codetree.ai #include #include #include #include using namespace std; int n, m, answer; int map[8][8]; int tmp[8][8]; bool isUsed[64]; int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, -1, 1, 0}; vector blank; vector..
[CodeTree] 병원 거리 최소화하기 (C++) [삼성 SW 역량테스트 기출] https://www.codetree.ai/training-field/frequent-problems/problems/min-of-hospital-distance 코드트리 | 코딩테스트 준비를 위한 알고리즘 정석 국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요. www.codetree.ai #include #include #include using namespace std; int n, m, answer = 987654321; int city[50][50]; bool isUsed[13]; vector patient; vector hospital; vector picked; void dfs(int depth, int start){ ..
[BOJ] 18429번 근손실(C++) https://www.acmicpc.net/problem/18429 18429번: 근손실 웨이트 트레이닝을 좋아하는 어떤 대학원생은, 현재 3대 운동 중량 500의 괴력을 소유하고 있다. 다만, 하루가 지날 때마다 중량이 K만큼 감소한다. 예를 들어 K=4일 때, 3일이 지나면 중량이 488로 www.acmicpc.net #include using namespace std; int n, k; int gain[8]; bool visited[8]; int weight = 500; int answer = 0; void dfs(int count){ if(count == n) answer++; else{ for(int i = 0; i < n; i++){ if(!visited[i]){ visited[i] = tru..