Codeup100000609问题 E: 【宽搜入门】巧妙取量

题目描述:

有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油。如果能就输出“yes”,并且说明最少倒几次,否则输出“no”。例如:10升油在10升的容器中,另有两个7升和3升的空容器,要求用这三个容器倒油,使得最后在abc三个容器中有一个刚好存有5升油,问最少的倒油次数是多少?(每次倒油,A容器倒到B容器,或者A内的油倒完,或者B容器倒满。

10 7 310 0 0)
(3 7 0:第一次
(3 4 3):第二次
(6 4 0):第三次
(6 1 3):第四次
(9 1 0):第五次
(9 0 1):第六次
(2 7 1):第七次
(2 5 3):第八次,出现5了。

输入:

有多组测试数据。
输入a,b,c, k四个正整数( 100 ≥ a > b > c≥1 , 1≤k< 100 )

输出:

如果能得到k就输出两行。
第一行“yes”,第二行为最少的次数
否则输出“no”

样例输入:

10 7 3 5

样例输出:

yes
8

实现代码:

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stack>
using namespace std;

struct Node {
	int a;
	int b;
	int c;
	int step;
};

int amax, bmax, cmax;//a,b,c的最大容量

Node modify(Node now, int i, int& m) {
	Node temp;
	switch (i) {
	case 1:		//A倒B 
		if (now.a == 0) {
			m = 0;
			return now;
		}
		if (now.a > bmax - now.b) {
			temp.a = now.a - (bmax - now.b);
			temp.b = bmax;
		}
		else {
			temp.a = 0;
			temp.b = now.b + now.a;
		}
		temp.c = now.c;
		temp.step = now.step + 1;
		break;
	case 2:		//A倒C
		if (now.a == 0) {
			m = 0;
			return now;
		}
		if (now.a > cmax - now.c) {
			temp.a = now.a - (cmax - now.c);
			temp.c = cmax;
		}
		else {
			temp.a = 0;
			temp.c = now.c + now.a;
		}
		temp.b = now.b;
		temp.step = now.step + 1;
		break;
	case 3:		//B倒A 
		if (now.b == 0) {
			m = 0;
			return now;
		}
		if (now.b > amax - now.a) {
			temp.b = now.b - (amax - now.a);
			temp.a = amax;
		}
		else {
			temp.b = 0;
			temp.a = now.a + now.b;
		}
		temp.c = now.c;
		temp.step = now.step + 1;
		break;
	case 4:		//B倒C 
		if (now.b == 0) {
			m = 0;
			return now;
		}
		if (now.b > cmax - now.c) {
			temp.b = now.b - (cmax - now.c);
			temp.c = cmax;
		}
		else {
			temp.b = 0;
			temp.c = now.c + now.b;
		}
		temp.a = now.a;
		temp.step = now.step + 1;
		break;
	case 5:		//C倒A
		if (now.c == 0) {
			m = 0;
			return now;
		}
		if (now.c > (amax - now.a)) {
			temp.c = now.c - (amax - now.a);
			temp.a = amax;
		}
		else {
			temp.c = 0;
			temp.a = now.a + now.c;
		}
		temp.b = now.b;
		temp.step = now.step + 1;
		break;
	case 6:		//C倒B 
		if (now.c == 0) {
			m = 0;
			return now;
		}
		if (now.c > bmax - now.b) {
			temp.c = now.c - (bmax - now.b);
			temp.b = bmax;
		}
		else {
			temp.c = 0;
			temp.b = now.b + now.c;
		}
		temp.a = now.a;
		temp.step = now.step + 1;
		break;
	}
	return temp;
}

int BFS(int nowa, int nowb, int nowc, int target) {
    int flag[101][101][101] = { 0 };//记录状态
	queue<Node> q;
	Node temp;
	temp.a = nowa;
	temp.b = nowb;
	temp.c = nowc;
	temp.step = 0;
	q.push(temp);
	flag[nowa][nowb][nowc] = 1;
	while (!q.empty()) {
		Node now = q.front();
		q.pop();
		if (now.a == target || now.b == target || now.c == target) {
			return now.step;
		}
		for (int i = 1; i <= 6; i++) {
			int m = 1;
			Node temp = modify(now, i, m);
			if (flag[temp.a][temp.b][temp.c] == 0 && m) {
				q.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
				if (temp.a == target || temp.b == target || temp.c == target) {
					return temp.step;
				}
			}
		}
	}
	return -1;
}

int main()
{
	int target;
	while (scanf("%d%d%d%d", &amax, &bmax, &cmax, &target) != EOF) {
		if (amax < target) {
			printf("no\n");
		}
		else {
			int a = BFS(amax, 0, 0, target);
			if (a != -1) {
				printf("yes\n%d\n", a);
			}
			else {
				printf("no\n");
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值