https://www.acmicpc.net/problem/11578
이런 탐색에는 재귀함수가 더 어울린다고 생각해서 재귀 함수를 이용해 풀었습니다.
재귀함수에 친해질 필요가 있겠네요.
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 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> using namespace std; bool stu[11][11] = {0}; int chk[11] = {0}; int ret = 11; int N = 0, M = 0; int solve(int start, int num); int main(){ int O,t; chk[0] = true; cin >> N >> M; for(int i = 1; i <= M; i++){ cin >> O; for(int j = 1; j <= O; j++){ cin >> t; stu[i][t] = true; } } solve(0,0); if(ret == 11) cout << -1; else cout << ret; } int solve(int start, int num){ bool chk2 = true; for(int i =1; i <= N; i++){ if(stu[start][i]) chk[i] +=1; } for(int i = 1; i <= N; i++){ if(chk[i]==0){ chk2 = false; break; } } if(chk2) return num; for(int i = start+1; i<=M; i++){ ret= min(ret,solve(i,num+1)); for(int j = 1; j <=N; j++) if(stu[i][j]) chk[j] -=1; } } | cs |
반응형
'프로그래밍 > 문제풀이' 카테고리의 다른 글
[etc] 백준 15828 Router (0) | 2018.12.28 |
---|---|
[etc] 백준 14612 김식당 (0) | 2018.12.28 |
[etc] 백준 11575 Affine Cipher (0) | 2018.12.24 |
[bfs] 백준 15900 나무 탈출 (2) | 2018.12.24 |
[etc] 백준 15903 카드 합체 놀이 (0) | 2018.12.22 |