USACO 2.1 Healthy Holsteins (DFS)

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
#define MAX_TYPES_VITAMINS 25
#define MAX_TYPES_FEEDS 15
int typesOfVitamins;
int minRequare[MAX_TYPES_VITAMINS + 1];
int typesOfFeeds;
int vitaminOfFeed[MAX_TYPES_FEEDS + 1][MAX_TYPES_VITAMINS + 1];
int currentVitamin[MAX_TYPES_VITAMINS + 1];
int stack[MAX_TYPES_FEEDS + 1];
int top;
int feedsEatten[MAX_TYPES_FEEDS + 1];
int minTypesOfFeedsEatten;

int enough(){
	int vitamin;
	for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
		if (currentVitamin[vitamin] < minRequare[vitamin])
			return 0;
	return 1;
}

void eat(int feed){
	if (top >= minTypesOfFeedsEatten)
		return;
	if (enough()){
		minTypesOfFeedsEatten = top;
		int i;
		for (i = 1; i <= top; i++)
			feedsEatten[i] = stack[i];
		return;
	}
	if (feed > typesOfFeeds)
		return;
	int vitamin;
	for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
		currentVitamin[vitamin] += vitaminOfFeed[feed][vitamin];
	stack[++top] = feed;
	eat(feed + 1);
	for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
		currentVitamin[vitamin] -= vitaminOfFeed[feed][vitamin];
	top--;
	eat(feed + 1);
}

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputx.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	scanf("%d", &typesOfVitamins);
	int vitamin;
	for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
		scanf("%d", &minRequare[vitamin]);

	scanf("%d", &typesOfFeeds);
	int feed;
	for (feed = 1; feed <= typesOfFeeds; feed++)
		for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
			scanf("%d", &vitaminOfFeed[feed][vitamin]);

	for (vitamin = 1; vitamin <= typesOfVitamins; vitamin++)
		currentVitamin[vitamin] = 0;
	top = 0;
	minTypesOfFeedsEatten = MAX_TYPES_FEEDS + 1;

	eat(1);

	printf("%d ", minTypesOfFeedsEatten);
	int i;
	for (i = 1; i <= minTypesOfFeedsEatten; i++)
		printf("%d ", feedsEatten[i]);
	printf("\n");

#if DEBUG
	}
#endif
	return 0;
}

你可能感兴趣的:(USACO,DFS,2.1,HEALTHY,Holsteins)