首页 > 代码库 > UVa 571 - Jugs

UVa 571 - Jugs

题目:给你两个杯子,每次可以给某个杯子灌满,清空,或者将一个杯子的水倒入另一个杯子;

            使得一个杯子为满或者另一个杯子为空,求最少的操作次数使得B杯中的水为目标值。

分析:搜索。韩信走马分油。

            设状态(a,b)为两倍的水容量,则最多1000000个状态,bfs求最短路(操作数最少)。

说明:很多年前写的代码,优化了一下(⊙_⊙)。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>

using namespace std;

/*操作编号: 
 *fill A    --------0
 *fill B    --------1
 *empty A   --------2
 *empty B   --------3
 *pour A B  --------4
 *pour B A  --------5
 */

int Ca,Cb,N;
int used[1001][1001]; /*  标记判重 */
typedef struct queue
{
	int a,b,p,o;
};
queue Q[1000001];

void output(int i)
{
	if (i < 0) return;
	else {
		output(Q[i].p);
		switch(Q[i].o) {
			case 0: cout << "fill A\n";   break;
			case 1: cout << "fill B\n";   break;
			case 2: cout << "empty A\n";  break;	
			case 3: cout << "empty B\n";  break;
			case 4: cout << "pour A B\n"; break;
			case 5: cout << "pour B A\n"; break;
		}
	}
}

void search(int Ca , int Cb , int T)
{
	memset(used , 0, sizeof(used));
	used[0][0] = 1;
	Q[0].a = Q[0].b = 0;Q[0].p = Q[0].o = -1;
	int move = 0,save = 1;
	while (move < save) {
		queue Now = Q[move ++];
		for (int i = 0 ; i < 6 ; ++ i) {
			queue New = Now;
				  New.o = i; New.p = move-1;
			switch(i) {
				case 0: if (Now.a != Ca && Now.b != Cb) 
							New.a = Ca;
						break;
				case 1: if (Now.a != Ca && Now.b != Cb) 
							New.b = Cb;
						break;
				case 2: if (Now.a && Now.b)
							New.a = 0;
						break;
				case 3: if (Now.a && Now.b)
							New.b = 0;
						break;
				case 4: if (Now.b != Cb)
							if (Now.b + Now.a <= Cb) {
								New.b = Now.a + Now.b;
								New.a = 0;
							}else {
								New.b = Cb;
								New.a = Now.a - Cb + Now.b;
							}
						break;
				case 5: if (Now.a != Ca)
							if (Now.a + Now.b <= Ca) {
								New.a = Now.b + Now.a;
								New.b = 0;
							}else {
								New.a = Ca;
								New.b = Now.b - Ca + Now.a;
							}
						break;
			}
			if (!used[New.a][New.b]) {
				used[New.a][New.b] = 1;
				Q[save ++] = New;
				if (New.b == T) {
					output(save-1);
					cout << "success\n"; 
					return;
				}
			}
		}
	}
}

int main()
{
	while (cin >> Ca >> Cb >> N)
		search(Ca , Cb , N);
	return 0; 
}


UVa 571 - Jugs