USACO 1.4 Mother's Milk (枚举 + DFS)

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
#define MAX_CAPACITY 20

int capacity[4];
int visited[MAX_CAPACITY + 1][MAX_CAPACITY + 1];
int count;

void pour(int state[4]){
	int from, to;
	for (from = 1; from <= 3; from++)
		for (to = 1; to <= 3; to++){
			if (from == to)
				continue;
			int milk[4];
			int bucket;
			for (bucket = 1; bucket <= 3; bucket++)
				milk[bucket] = state[bucket];
			int temp = state[to] + state[from];
			if (temp > capacity[to]){
				milk[from] = temp - capacity[to];
				milk[to] = capacity[to];
			} else {
				milk[from] = 0;
				milk[to] = temp;
			}
			if (visited[ milk[1] ][ milk[3] ] == 1)
				continue;
			else {
				count++;
				visited[ milk[1] ][ milk[3] ] = 1;
				pour(milk);
			}
		}//end of for (to = 1; to <= 3; to++)
}

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

	int bucket;
	for (bucket = 1; bucket <= 3; bucket++)
		scanf("%d", &capacity[bucket]);

	int capacityA = capacity[1];
	int capacityC = capacity[3];
	int milkA, milkC;
	for (milkA = 0; milkA <= capacityA; milkA++)
		for (milkC = 0; milkC <= capacityC; milkC++)
			visited[milkA][milkC] = 0;

	int initialState[4] = {0, 0, 0, capacity[3]};
	//可根据count判断最后应该输出空格还是回车
	count = 0;
	pour(initialState);
	
	for (milkC = 0; milkC <= capacityC; milkC++)
		for (milkA = 0; milkA <= capacityA; milkA++){
			if (visited[milkA][milkC] == 1 && milkA == 0){
				count--;
				printf("%d%c", milkC, count == 0 ? '\n' : ' ');
				break;
			}
		}
		
#if DEBUG
	}
#endif
	return 0;
}


 
 
 

你可能感兴趣的:(枚举,1.4,USACO,DFS,milk,Mothers)