码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10603 Fill (BFS)

时间:2015-02-14 17:33:02      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:acm   c语言   uva   

                         uva 10603 Fill


There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third

is completely filled with water. It is allowed to pour water from one jug into another until either the first one is empty or the second one is full. This operation can be performed zero, one or more times.

 

You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should find a smaller amount of water d‘ < d which is closest to d and for which d‘ liters could be produced. When d‘ is found, your program should compute the least total amount of poured water needed to produce d‘ liters in at least one of the jugs.

 

Input

The first line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input containing four space separated integers - a, b, c and d.

 

Output

The output consists of two integers separated by a single space. The first integer equals the least total amount (the sum of all waters you pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations, or equals the closest smaller value d‘ that your program has found.

 

Sample Input

Sample Output

2                                                                                         

2 3 4 2

96 97 199 62

2 2                                                                                    

9859 62

 


题目大意:第一行数据为数据组数,接下来几行为数据,每组数据包括四个数字 :三个水杯的容量, 以及目标水量,初始状态为第一第二水杯为空,三号杯满。进行一次倒水操作当且仅当倒出水杯空或者接收水杯满才停止,求到达目标状态的最小水移动量,当无法到达目标状态时,求最接近的状态。

解题思路:BFS, 因为可能无法到达,所以需要一个额外的数组来记录到达各种状态所需的最少水量。



#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct state {
	int a[3], cost;
	bool operator < (const state& a) const {
		return this -> cost > a.cost;
	}
};
state temp;
int jugs[3], d, vis[205][205], ans[205], head, tail;
void update(state s) { 
	for (int i = 0; i < 3; i++) {
		int t2 = s.a[i];
		if (ans[t2] < 0 || s.cost < ans[t2]) { //记录到达t2状态(出现装有t2水量的水杯)所需的最小水量
			ans[t2] = s.cost;
		}
	}
}
void BFS() {  
	priority_queue<state> q;
	state start;  
	start.cost = 0;  
	start.a[0] = 0;  
	start.a[1] = 0;  
	start.a[2] = jugs[2];  
	q.push(start);  
	vis[0][0] = 1;   

	while(!q.empty()) {  
		temp = q.top();  
		q.pop();  
		update(temp);  //更新记录
		if(ans[d] >= 0)	break;  

		for(int i = 0; i < 3; i++) {  
			if (temp.a[i] == 0) continue;

			for(int j = 0; j < 3; j++) {  
				if (i == j) continue;
				if (temp.a[j] == jugs[j]) continue;

				int m = min(temp.a[i], jugs[j] - temp.a[j]);  
				state temp2 = temp;  
				temp2.cost += m;  
				temp2.a[i] -= m;  
				temp2.a[j] += m;  

				if(!vis[temp2.a[0]][temp2.a[1]]) {  
					vis[temp2.a[0]][temp2.a[1]] = 1;  
					q.push(temp2);  
				}  
			}  
		}  
	}  

}  
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d %d %d", &jugs[0], &jugs[1], &jugs[2], &d);
		memset(vis, 0, sizeof(vis));
		memset(ans, -1, sizeof(ans));
		BFS();
		while (d >= 0) {
			if (ans[d] >= 0) { //若不能到达目标状态,就寻找最接近并且可达状态
				printf("%d %d\n", ans[d], d);
				break;
			}
			d--;
		}
	}
	return 0;
}



uva 10603 Fill (BFS)

标签:acm   c语言   uva   

原文地址:http://blog.csdn.net/llx523113241/article/details/43818003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!