USACO Section 3.4: Raucous Rockers

简单的dfs题目

 1 /*

 2 ID: yingzho1

 3 LANG: C++

 4 TASK: rockers

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ifstream fin("rockers.in");

26 ofstream fout("rockers.out");

27 

28 int N, T, M;

29 int songs[21];

30 int maxnum = 0;

31 

32 void dfs(int leftvol, int diskdep, int songdep, int recsong) {

33     if (diskdep == M) {

34         maxnum = max(maxnum, recsong);

35         return;

36     }

37     if (songdep == N) {

38         maxnum = max(maxnum, recsong);

39         return;

40     }

41     if (leftvol >= songs[songdep]) dfs(leftvol-songs[songdep], diskdep, songdep+1, recsong+1);

42     else dfs(T, diskdep+1, songdep, recsong);

43     dfs(leftvol, diskdep, songdep+1, recsong);

44 }

45 

46 int main()

47 {

48     fin >> N >> T >> M;

49     for (int i = 0; i < N; i++) fin >> songs[i];

50     dfs(T, 0, 0, 0);

51     fout << maxnum << endl;

52 

53     return 0;

54 }

 

你可能感兴趣的:(USACO)