北大ACM试题1010-DFS

    真是无语了。。。默默发现距离上篇博客又是一个月过去了。。。这段时间都没怎么好好写,是因为。。。。到处看房去了。。。帝都这种寸土寸金的地方,买个房简直是要了老命了。。。当然要了我的老命也是买不起的,主要还是要了老爸老妈的老命。。。60平米的二手房290万。。。首付是能借的都借了,不能借的也借了。。。然后每个月还要还8000的贷款。。。是时候该换一波工作了。。要不换了房贷只能喝西北风了。。。但是毕竟有一个可以落脚的地方了,不用每天花一个半小时去上班。。。

    闲话就说到这里吧,上次说到DFS的初探,那么现在就是直接套用到这道题目里面就可以了,大神提出来的框架真心是犀利爆了,所谓的万变不离其宗吧。。。算法如下,原来是可以插入代码段的,之前的都丑爆了。。。



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static int MaxCount = 4;
	static int[] solution ;//最终解,0-3是取第几种邮票,4是邮票种类,5是张数,6是最大面额
	static int[] tempSolution ;//临时解
	static boolean isTie ;//是否为tie
	// 获得邮票的种类,一定要注意取的是第几种邮票,而不是面额,因为面额有可能相同,但是也算2种
	public static int getStampType(int[] data) {
		if(data[0] == 0){
			return 0;
		}
		int type = 1;
		int temp = 0;
		for (int i = 1; i < MaxCount; i++) {
			if (data[i] != data[temp] && data[i] != 0) {
				type++;
			}
			temp = i;
		}
		return type;
	}
	//获得邮票的数量
	public static int getStampCount(int[] data) {
		int count = 0;
		for(int i=0;i<MaxCount;i++){
			if(data[i] != 0){
				count++;
			}
		}
		return count;
	}
	//获得最大面额,因为已经排好序了,直接取最右边即可
	public static int getStampMax(int[] stamp,int[] data){
		for(int i=MaxCount-1;i>=0;i--){
			if(data[i] !=0){
				return stamp[data[i]];
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		List<String> output = new ArrayList<String>();
		while(cin.hasNext()){
			String stampss = cin.nextLine();
			String[] stamps = stampss.split(" ");
			int[] stamp = new int[stamps.length];
			for(int i=0;i<stamps.length;i++){
				stamp[i] = Integer.parseInt(stamps[i]);
			}
			//对输入的邮票进行排序,小心第一个是0。。。
			Arrays.sort(stamp);
			String[] customers = cin.nextLine().split(" ");
			for(int i=0;i<customers.length-1;i++){
				//一定记得要初始化一下
				solution = new int[7];
				tempSolution = new int[7];
				
				int customer = Integer.parseInt(customers[i]);
				dfs(customer,0,1,stamp);
				//输出
				String o = customer+" ";
				if(solution[5] == 0){
					o += "---- none";
				}else{
					o += "("+solution[4]+"):";
					if(isTie){
						o += " tie";
					}else{
						for(int j=0;j<4;j++){
							if(solution[j] != 0){
								o += " "+stamp[solution[j]];
							}
						}
					}
				}
				output.add(o);
			}
		}
		for(int i=0;i<output.size();i++){
			System.out.println(output.get(i));
		}
		cin.close();
	}
	
	public static void dfs(int need , int deep ,int pre ,int[] stamp){
		if(deep == MaxCount+1){
			//邮票张数超过4
			return;
		}
		//框架中的更新解模块,当need为0时更新
		if(need == 0){
			//更新临时解的状态
			tempSolution[4] = getStampType(tempSolution);
			tempSolution[5] = getStampCount(tempSolution);
			tempSolution[6] = getStampMax(stamp,tempSolution);
			//如果为第一次
			solution[5] = getStampCount(solution);
			if(solution[5] == 0){
				isTie = false;
				solution=tempSolution.clone();
				return ;
			}
			solution[4] = getStampType(solution);
			solution[6] = getStampMax(stamp,solution);
			//对临时解和最优解比较,依次为种类最多,张数最少,面额最大
			if(tempSolution[4] > solution[4]){
				isTie = false;
				solution = tempSolution.clone();
			}else if(tempSolution[4] == solution[4]){
				if(tempSolution[5] < solution[5]){
					isTie = false;
					solution = tempSolution.clone();
				}else if(tempSolution[5] == solution[5]){
					if(tempSolution[6] > solution[6]){
						isTie = false;
						solution = tempSolution.clone();
					}else if(tempSolution[6] == solution[6]){
						isTie = true;
					}
				}
			}
		}
		//每次从i开始搜索,剪枝,同时避免错误的tie
		for(int i=pre;i<stamp.length;i++){
			if(need >= stamp[i]){
				//一定注意保持的是第几种邮票,而need减去的是邮票的面额
				tempSolution[deep] = i;
				dfs(need - stamp[i],deep+1,i,stamp);
				//回溯
				tempSolution[deep] = 0;
			}else{
				return;
			}
		}
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值