본문 바로가기

Coding/백준

1759번 암호 만들기

 백트래킹 기본 문제.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>
#define visited true
#define Non_visited false
#define MAX 16
 
using namespace std;
 
char graph[MAX];
int L,C;
 
void dfs(int i, int consonants, int vowels, string str){
    if(str.size()==L){
        if(consonants<2 || vowels<1){
            return;
        }
        cout << str << endl;
    }
    
    while(i<C){
        if(graph[i]=='a' || graph[i]=='e' || graph[i]=='i' || graph[i]=='o' || graph[i]=='u'){
            dfs(i+1,consonants,vowels+1,str+graph[i]);
        }
        else{
            dfs(i+1,consonants+1,vowels,str+graph[i]);
        }
        i++;
    }
}
 
int main(){
    cin >> L >> C;
    
    for(int i=0;i<C;i++){
        cin >> graph[i];
    }
    
    sort(graph,graph+C);
    
    dfs(0,0,0,"");
}
 
cs

'Coding > 백준' 카테고리의 다른 글

11051번 이항 계수 2  (0) 2020.02.17
11722번 가장 긴 감소하는 부분 수열  (0) 2020.02.16
2959번 거북이  (0) 2020.02.13
11048번 이동하기  (0) 2020.02.13
2146번 다리 만들기  (0) 2020.02.12