250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 볼륨 만들기
- 중복조합
- 파티션 크기 조정
- 알고리즘
- java
- Bfs와DFS
- 에라토스테네스의채
- 백준
- 주사위굴리기2
- 자바 코테
- 알고리즘개념
- 순열
- 백준15652
- 코테
- 23288
- BFS
- 전화번호속의암호
- 정보처리기사
- 정올 1620
- 완탐
- 완전탐색
- 백준13458
- 코테준비
- 재귀함수
- 삼성역테
- D드라이브생성
- 중복순열
- 백준2251
- 자바
- N과M
Archives
- Today
- Total
뚱땅뚱땅
[알고리즘] 알고스팟 PICNIC 문제 본문
728x90
알고리즘을 통한 문제 해결전략 6과
- 재귀 함수 이용
- 중복으로 세는 경우, 사전순으로 하여 답 하나만 세도록 한다.
#include <iostream>
#include <vector>
using namespace std;
int n; // the number of students
bool areFriends[10][10] = { false, };
int countPairings(bool taken[10]);
int main() {
int testcase;
int pair;
int f1, f2;
vector<int> answer;
cin >> testcase;
for (int i = 0; i < testcase; i++) {
//areFriends false로 다시 초기화
for (int m = 0; m < 10; m++) {
for (int p = 0; p < 10; p++) {
areFriends[m][p] = false;
}
}
//taken[i] : i번쨰 학생이 짝을 찾았으면 true, 아니면 false
bool taken[10] = { false, };
cin >> n >> pair;
for (int j = 0; j < pair; j++) {
cin >> f1 >> f2;
areFriends[f1][f2] = true;
areFriends[f2][f1] = true;
}
answer.push_back(countPairings(taken));
}
for (vector<int>::iterator itr = answer.begin(); itr != answer.end(); ++itr) {
cout << *itr << endl;
}
}
int countPairings(bool taken[10]) {
//남은 학생들 중 가장 번호가 빠른 학생 찾기
int firstFree = -1;
for (int i = 0; i < n; ++i) {
if (!taken[i]) {
firstFree = i;
break;
}
}
//base case: 모든 학생이 짝을 찾았으면 한가지 방법 찾았으니 종료
if (firstFree == -1) return 1;
int ret = 0;
//이 학생과 짝지을 학생수 결정
for (int pairWith = firstFree + 1; pairWith < n; ++pairWith) {
if (!taken[pairWith] && areFriends[firstFree][pairWith]) {
taken[firstFree] = taken[pairWith] = true;
ret += countPairings(taken);
taken[firstFree] = taken[pairWith] = false;
}
}
return ret;
}
728x90
'아카이브' 카테고리의 다른 글
[2020 정보처리기사 필기] 2.1.35 데이터저장소/데이터베이스/DBMS (0) | 2020.03.27 |
---|---|
[2020 정보처리기사 필기] 2.1.34 자료구조 (0) | 2020.03.27 |
[알고리즘] 알고스팟 BOGGLE문제 (0) | 2020.03.26 |
[Android Studio] ‘cannot resolve symbol’ error (0) | 2020.02.13 |
[Flutter] 기본 개념 + 예제 코드 작성 (0) | 2020.01.10 |
Comments