* 초안
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 | #include<iostream> using namespace std; int S; int time = 1; void solve(int queue, int monitor) { time++; // ending 조건 if (S == monitor) return; // return 조건 if (queue == 0) return; if (monitor == 1) return; // C(Copy, 복사) solve(monitor, monitor); // P(Paste, 붙여넣기) solve(queue, monitor + queue); // D(Delete, 삭제) solve(queue, monitor - 1); } int main() { cin >> S; solve(0, 1); cout << time; return 0; } | cs |
당연히 안될거라 생각했는데, 왜 안되는진 모르겟당..
* 가지 3개 만들어서 재귀, 백트래킹이 되어버렸넹? BFS짤라했는데..!
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 54 55 | #include<iostream> using namespace std; int S; int time = 0; void solve(int queue, int monitor, int copyCnt) { time++; // ending 조건 if (S == monitor) return; // return 조건 if (queue == 0) return; if (monitor == 0) return; if (copyCnt >= 2) { return; } // C(Copy, 복사) solve(monitor, monitor, copyCnt + 1); time--; // P(Paste, 붙여넣기) solve(queue, monitor + queue, 0); time--; // D(Delete, 삭제) solve(queue, monitor - 1, 0); time--; } int main() { cin >> S; // C solve(1, 1, 1); // P solve(0, 1, 0); // D solve(0, 0, 0); cout << time; return 0; } | cs |
'알고리즘 > BOJ' 카테고리의 다른 글
14503 로봇청소기 (0) | 2018.03.07 |
---|---|
1211 Ladder2 (0) | 2018.03.05 |
1987 알파벳 (+문자배열에 문자열 저장하기. 성공) (0) | 2018.02.22 |
2638 치즈 (미완) (0) | 2018.02.21 |
2644 촌수계산 BFS (성공) (0) | 2018.02.20 |