NYOJ 21 三个水杯 (BFS)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=21

三个水杯

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
输入
第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
输出
每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
样例输入
2
6 3 1
4 1 1
9 3 2
7 1 1
样例输出
3
-1

经典广搜题。

代码太丑。。

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int A, B, C;
int EA, EB, EC;
struct node {
	int a, b, c;
	int cnt;
};
int vis[110][110][110];
void move(node& now, node& next, int c) {
	if(c - now.b >= now.a) {
		next.a = 0;
		next.b = now.b + now.a;
	}
	else {
		next.a = now.a - (c - now.b);
		next.b = c;
	}
	next.cnt = now.cnt + 1;

}
int bfs() {
	node now, next;
	now.a = A, now.b = 0, now.c = 0, now.cnt = 0;
	queue<node> q;
	q.push(now);
	int i;
	vis[now.a][now.b][now.c] = 1;
	while(!q.empty()) {
		now = q.front();
		q.pop();
		if(now.a == EA && now.b == EB && now.c == EC) {
			return now.cnt;
		}
		next = now;
		if(now.a != 0 && now.b < B) {
			if(B - now.b >= now.a) {
				next.a = 0;
				next.b = now.b + now.a;
			}
			else {
				next.a = now.a - (B - now.b);
				next.b = B;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
		next = now;
		if(now.a != 0 && now.c < C) {
			if(C - now.c >= now.a) {
				next.a = 0;
				next.c = now.c + now.a;
			}
			else {
				next.a = now.a - (C - now.c);
				next.c = C;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
		next = now;
		if(now.b != 0 && now.a < A) {
			if(A - now.a >= now.b) {
				next.b = 0;
				next.a = now.a + now.b;
			}
			else {
				next.b = now.b - (A - now.a);
				next.a = A;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
		next = now;
		if(now.c != 0 && now.a < A) {
			if(A - now.a >= now.c) {
				next.c = 0;
				next.a = now.a + now.c;
			}
			else {
				next.c = now.c - (A - now.a);
				next.a = A;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
		next = now;
		if(now.b != 0 && now.c < C) {
			if(C - now.c >= now.b) {
				next.b = 0;
				next.c = now.c + now.b;
			}
			else {
				next.b = now.b - (C - now.c);
				next.c = C;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
		next = now;
		if(now.c != 0 && now.b < B) {
			if(B - now.b >= now.c) {
				next.c = 0;
				next.b = now.b + now.c;
			}
			else {
				next.c = now.c - (B - now.b);
				next.b = B;
			}
			next.cnt = now.cnt + 1;
			if(!vis[next.a][next.b][next.c]) {
				vis[next.a][next.b][next.c] = 1;
				q.push(next);
			}
		}
	}
	return -1;
}
int main() {
	int N;
	scanf("%d", &N);
	while(N--) {
		scanf("%d %d %d", &A, &B, &C);
		scanf("%d %d %d", &EA, &EB, &EC);
		memset(vis, 0, sizeof(vis));
		printf("%d\n", bfs());
	}
	return 0;
}        


你可能感兴趣的:(NYOJ 21 三个水杯 (BFS))