uva 10603 Fill

题目:Fill


题意:有3个容积为a,b,c的杯子,最初a、b为空,c中装满水。每次可以从一个杯子向另一个杯子倒水,但只有把一个杯子倒空或把另一个杯子倒满时才能停止。如果要使其中一个杯子中有d升水,求最少倒水的的体积。若无法满足 ,则目标水量要比d小且和d尽量接近。


思路:

bfs遍历。

由于所说为体积最小,所以要一直将图遍历完才能遭到最优解。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<sstream>
using namespace std;
#define n 3

struct Node {
	int x[n];
	Node() {}
	Node(int one,int two,int three) {
		x[0]=one,x[1]=two,x[2]=three;
	}
	bool operator <(const Node& other) const {
		for(int i=0; i<n; i++) {
			if(x[i]<other.x[i]) return true;
			if(x[i]>other.x[i]) return false;
		}
		return false;
	}
};

int d;
Node M;

struct Pair {
	int step,D;
	Pair() {
		step=0,D=0;
	}
	Pair(int x[],int S) {
		step=S;
		D=d;
		for(int i=0; i<n; i++) {
			if(x[i]<=d) D=min(D,d-x[i]);
		}
	}
	Pair(int one,int two) {
		step=one,D=two;
	}
	bool operator <(const Pair& other) const {
		if(D<other.D||(D==other.D&&step<other.step)) return true;
		return false;
	}
};

void bfs(Node IN,map<Node,Pair>& mp) {
	vector<Node> que;
	int t=0;
	que.push_back(IN);
	mp[IN]=Pair(IN.x,0);
	while(t<que.size()) {
		Node head=que[t];
		t++;
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++) 
				if(i!=j&&head.x[i]!=0&&head.x[j]!=M.x[j]) {
					Node ch=head;
					int move;
					if(ch.x[i]>=M.x[j]-ch.x[j]) move=M.x[j]-ch.x[j];
					else move=ch.x[i];
					ch.x[i]-=move,ch.x[j]+=move;
					if(!mp.count(ch)||mp[head].step+move<mp[ch].step) {
						que.push_back(ch);
						mp[ch]=Pair(ch.x,mp[head].step+move);
					}
				}
	}
}

void find(map<Node,Pair> mp) {
	vector<Pair> vec;
	for(map<Node,Pair>::iterator it=mp.begin(); it!=mp.end(); it++) vec.push_back(it->second);
	sort(vec.begin(),vec.end());
	printf("%d %d\n",vec[0].step,d-vec[0].D);
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int A,B,C;
		scanf("%d%d%d%d",&A,&B,&C,&d);
		M=Node(A,B,C);
		map<Node,Pair> mp;
		bfs(Node(0,0,C),mp);
		find(mp);
	}
	return 0;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值