USACO - 1.4.4 - Mother's Milk

 


原创文章转载请注明出处

摘要: 枚举,暴力破解

一. 题目翻译

1. 描述:
          农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的。有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌桶装满或原桶空了。当然每一次灌注都是完全的。由于节约, 牛奶不会有丢失。
          写一个程序去帮助农民找出当A桶是空的时候,C桶中牛奶所剩量的所有可能性。


2. 格式:

          INPUT FORMAT:

          (file milk3.in)
          单独的一行包括三个整数A,B和C。

          OUTPUT FORMAT:

          (file milk3.out)
          只有一行,升序地列出当A桶是空的时候,C桶牛奶所剩量的所有可能性。


3. SAMPLE:
          SAMPLE INPUT:
2 5 10
          SAMPLE OUTPUT:
5 6 7 8 9 10
          
二.  题解
1. 题意理解(将问题分析清楚,大致用什么思路):
          基本思路,用BFS逐层产生可能有的A,B,C三个桶的牛奶数量。如果已经产生过了则不用进入bfs的队列了,所以我们用一个hash表记录所有已经产生的状态。
          如果bfs的队列当中没有元素了,则程序结束,输出所有已经产生的结果。


三.  代码

/*
ID: fightin1
LANG: JAVA
TASK: milk3
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class milk3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Scanner in = new Scanner(new BufferedReader(new FileReader("milk3.in")));
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milk3.out")));
			
			LinkedList<status> list = new LinkedList<status>();//bfs队列
			HashSet<status> hash = new HashSet<status>();//记录已经产生过的a,b,c三个桶的容量状态
			HashSet<Integer> result = new HashSet<Integer>();//记录已经产生的c桶的状态
			
			int a = in.nextInt();
			int b = in.nextInt();
			int c = in.nextInt();
			status init = new status(0,0,c);
			list.add(init);
			
			while (list.size()!=0){//编列bfs队列
				status temp = list.remove(0);
				if (hash.contains(temp)){
					//doNothing
				} else {
					hash.add(temp);
					//如果a桶的容量为空,则在结果集当中加入c的状态
					if (temp.a==0){
						result.add(temp.c);
					}
					//遍历所有可能的倒牛奶方式
					//a->b
					if (temp.a >= b-temp.b){
						status node = new status(temp.a-b+temp.b,b,temp.c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else{
						status node = new status(0,temp.b+temp.a,temp.c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					//a->c
					if (temp.a >= c-temp.c){
						status node = new status(temp.a-c+temp.c,temp.b,c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else{
						status node = new status(0,temp.b,temp.c+temp.a);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					//b->a
					if (temp.b >= a-temp.a){
						status node = new status(a,temp.b-a+temp.a,temp.c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else{
						status node = new status(temp.a+temp.b,0,temp.c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					//b->c
					if (temp.b >= c-temp.c){
						status node = new status(temp.a,temp.b-c+temp.c,c);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else {
						status node = new status(temp.a,0,temp.c+temp.b);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					
					//c->a
					if (temp.c >= a-temp.a){
						status node = new status(a,temp.b,temp.c-a+temp.a);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else{
						status node = new status(temp.a+temp.c,temp.b,0);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					
					//c->b
					if (temp.c >= b-temp.b){
						status node = new status(temp.a,b,temp.c-b+temp.b);
						if (!hash.contains(node)){
							list.add(node);
						}
					}else {
						status node = new status(temp.a,temp.b+temp.c,0);
						if (!hash.contains(node)){
							list.add(node);
						}
					}
					
				}
			}
			Integer[] out = new Integer[result.size()];
			result.toArray(out);
			Arrays.sort(out);
			StringBuilder sb = new StringBuilder();
			for (int i = 0;i<out.length;i++){
				sb.append(out[i]+" ");
			}
			sb.deleteCharAt(sb.length()-1);
			pw.println(sb);
			pw.close();
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class status {
	int a ;
	int b ;
	int c ;
	
	public status(int a ,int b ,int c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	
	@Override
	public boolean equals(Object obj) {
		status sta = (status)obj;
		if (this.a == sta.a && this.b == sta.b && this.c == sta.c){
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return a*1 + b*2 + c*3;
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值