HDU 4269 Defend Jian Ge 解题报告

水模拟,注意以下几个条件就可以了。

1、格子数只有6个,如果当格子满了就不可以买东西了,即使格子里面有consume物品也不能买相同的consume物品,但是可以合成物品,但是不能合成不需要合成物的物品(因为格子已经满了,放不下了)

2、一件mixture的价格是该物品的初始价格+合成组件的价格,这里我在代码中是先用DFS来得到所有mixture物品的价格

3、每次只能卖一件normal或mixture物品,但是consume物品被卖时是一个格子里的全部被卖掉,卖物品的价钱等于物品的原价

4、When a mixture equipment is get, the mixture equipment of recipe in you backpack will disappear.

5、 If one operation is illegal, there is nothing happened in your backpack and gold.

6、注意合成物品时可能需要的某种物品的个数是0个,Format: str num: str1 num1, str2 num2, ... , strn numn (num1 + num2 + … +numn<=6 and numi >= 0 (1 <= i <= n)),注意numi=0的时候

7、当金币不够的时候是不可以买东西的


源码如下,比较长

import java.util.*;

public class Main {
	/**
	 * @by xkey
	 */
	public static Map<String,Integer>mixValue;
	
	public static int dfs(Map<Map<String,Integer>,List<Map<String,Integer>>> mixMap,
			Map<String,Integer>key,Map<String,Integer>map1){
			
			Iterator ss = key.entrySet().iterator();
			String key1 = "";
			while(ss.hasNext()){
				Map.Entry entry2 = (Map.Entry) ss.next(); 
				key1 = (String)entry2.getKey();
			}
			Iterator mixItr = mixMap.entrySet().iterator();
			Map<String,Integer> getKey = new HashMap<String,Integer>();
			while(mixItr.hasNext()){
				boolean flag = false;
				Map.Entry en = (Map.Entry) mixItr.next();
				Map<String,Integer> findKey = (Map<String,Integer>)en.getKey();
				Iterator kk = findKey.entrySet().iterator();
				String mixString = "";
				int mixValue2 = 0;
				while(kk.hasNext()){
					Map.Entry ent = (Map.Entry)kk.next();
					mixString = (String)ent.getKey();
					mixValue2 = (Integer)ent.getValue();
					if(mixString.equals(key1) == true){
						flag = true;
						getKey = findKey;
						break;
					}
				}
				if(flag == true){
					break;
				}
			}
			List<Map<String,Integer>> val = (List<Map<String,Integer>>)mixMap.get(getKey);
			Iterator tt = getKey.entrySet().iterator();
			int value = 0;
			while(tt.hasNext()){
				Map.Entry entry2 = (Map.Entry) tt.next(); 
				value = (Integer)entry2.getValue();
			}
			if(val.size() == 0){
				if(mixValue.containsKey(key1)){
					int v = mixValue.get(key1);
					mixValue.put(key1, v+value);
				}else{
					mixValue.put(key1, value);
				}
				return mixValue.get(key1);
			}else{
				Iterator<Map<String,Integer>> itrea = val.iterator();
				int totalValue = 0;
				totalValue += value;
				if(val.size() == 0){
					mixValue.put(key1, totalValue);
				}else{
					while(itrea.hasNext()){
						Map<String,Integer> xkey = itrea.next();
						Iterator xkeyItr = xkey.entrySet().iterator();
						String xkeyStr2 = "";
						int xkeyVal = 0;
						while(xkeyItr.hasNext()){
							Map.Entry entry2 = (Map.Entry) xkeyItr.next(); 
							xkeyStr2 = (String)entry2.getKey();
							xkeyVal = (Integer)entry2.getValue();
						}
						if(map1.containsKey(xkeyStr2)){
							totalValue += xkeyVal * map1.get(xkeyStr2);
						}else{
							if(mixValue.containsKey(xkeyStr2)){
								totalValue += xkeyVal * mixValue.get(xkeyStr2);
							}else{
								totalValue += xkeyVal * dfs(mixMap,xkey,map1);
							}
						}
					}
					if(mixValue.containsKey(key1)){
						int v = mixValue.get(key1);
						mixValue.put(key1, v+totalValue);
					}else{
						mixValue.put(key1, totalValue);
					}
				}
				return mixValue.get(key1);
				//mixValue.put(key1,totalValue);
			}
			
	}
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n1,n2,n3,m,num,cnt=0;
		while(cin.hasNext()){
			cnt++;
			String [] mix = new String[100];
			String [] mix2 = new String[100];
			n1 = cin.nextInt();
			String str;
			String mixture1,mixture2;
			Map<String,Integer>map1 = new HashMap<String,Integer>();
			
			mixValue = new HashMap<String,Integer>();
			
			Map<Map<String,Integer>,List<Map<String,Integer>>> mixMap = 
				new HashMap<Map<String,Integer>,List<Map<String,Integer>>>();
			Map<String,Integer> map4 = new HashMap<String,Integer>();
			
			Map<String,Integer> own1 = new HashMap<String,Integer>();
			Map<String,Integer> own2 = new HashMap<String,Integer>();
			
			int grid1 = 0;
			
			for(int i = 0 ; i < n1 ; i ++){
				str = cin.next();
				num = cin.nextInt();
				map1.put(str,num);
			}
			
			Iterator itr1 = map1.entrySet().iterator();
			
			n2 = cin.nextInt();
			cin.nextLine();
			for(int i = 0 ; i < n2 ; i ++){
				str = cin.nextLine();
				mix = str.split(": ");
				Map<String,Integer>map2 = new HashMap<String,Integer>();
				if(mix.length >= 2){
					mixture1 = mix[0];
					mixture2 = mix[1];
					mix = mixture1.split(" ");
					map2.put(mix[0], Integer.parseInt(mix[1]));
					mix = mixture2.split(", ");
					List<Map<String,Integer>> mixList = new ArrayList<Map<String,Integer>>();
					for(int j = 0 ; j < mix.length ; j ++){
						mix2 = mix[j].split(" ");
						Map<String,Integer>map3 = new HashMap<String,Integer>();
						map3.put(mix2[0], Integer.parseInt(mix2[1]));
						mixList.add(map3);
					}
					mixMap.put(map2, mixList);
				}
				else{
					mixture1 = mix[0];
					List<Map<String,Integer>> mixList = new ArrayList<Map<String,Integer>>();
					mix = mixture1.split(" ");
					map2.put(mix[0], Integer.parseInt(mix[1]));
					mixMap.put(map2, mixList);
				}
			}
			
			Iterator mixItr = mixMap.entrySet().iterator();
			
			while(mixItr.hasNext()){
				Map.Entry entry = (Map.Entry) mixItr.next(); 
				Map<String,Integer> key = (Map<String,Integer>)entry.getKey(); 
				
				List<Map<String,Integer>> val = (List<Map<String,Integer>>)entry.getValue(); 
				Iterator kk = key.entrySet().iterator();
				String mixString = "";
				int mixValue2 = 0;
				while(kk.hasNext()){
					Map.Entry en = (Map.Entry)kk.next();
					mixString = (String)en.getKey();
					mixValue2 = (Integer)en.getValue();
				}
				Iterator<Map<String,Integer>> itrea = val.iterator();
				int totalValue = 0;
				totalValue += mixValue2;
				if(val.size() == 0){
					mixValue.put(mixString, totalValue);
				}
				else{
					while(itrea.hasNext()){
						Map<String,Integer> xkey = itrea.next();
						Iterator xkeyItr = xkey.entrySet().iterator();
						String xkeyStr = "";
						int xkeyVal = 0;
						while(xkeyItr.hasNext()){
							Map.Entry entry2 = (Map.Entry) xkeyItr.next(); 
							xkeyStr = (String)entry2.getKey();
							xkeyVal = (Integer)entry2.getValue();
						}
						if(map1.containsKey(xkeyStr)){
							totalValue += xkeyVal * map1.get(xkeyStr);
						}else{
							if(mixValue.containsKey(xkeyStr)){
								totalValue += xkeyVal * mixValue.get(xkeyStr);
							}else{
								//xkey can't be find -> dfs
								int tempValue = dfs(mixMap,xkey,map1);
								totalValue += xkeyVal * tempValue;
							}
						}
						mixValue.put(mixString,totalValue);
					}
				}
			}
			
//			Iterator valItr = mixValue.entrySet().iterator();
//			System.out.println(mixValue.size());
//			while(valItr.hasNext()){
//				Map.Entry entry = (Map.Entry)valItr.next();
//				System.out.println("mixValue "+entry.getKey()+" "+entry.getValue());
//			}
			
			Iterator itr2 = mixMap.entrySet().iterator();
			n3 = cin.nextInt();
			for(int i = 0 ; i < n3 ; i ++){
				str = cin.next();
				num = cin.nextInt();
				map4.put(str, num);
			}
			
			Iterator itr4 = map4.entrySet().iterator();
			
			int gold = 0;
			m = cin.nextInt();
			for(int i = 0 ; i < m ; i ++){
				str = cin.next();
				String temp = str.substring(1);
				if(str.charAt(0) == '+'){
					if(str.charAt(1) >= '0' && str.charAt(1) <= '9'){
						gold += Integer.valueOf(temp);
					}else{
						if(map1.containsKey(temp) && grid1 < 6){
							int value = map1.get(temp);
							if(value <= gold){
								gold -= value;
								if(own1.containsKey(temp)){
									num = own1.get(temp);
									own1.put(temp, num+1);
								}else{
									own1.put(temp, 1);
								}
								grid1 ++;
							}
						}
						//consume
						else if(map4.containsKey(temp) && grid1 < 6){
							int value = map4.get(temp);
							if(value <= gold){
								gold -= value;
								if(own1.containsKey(temp)){
									num = own1.get(temp);
									own1.put(temp, num+1);
								}else{
									own1.put(temp, 1);
									grid1++;
								}
							}
						}else{
							//check mixMap must be have,and it can be got
							boolean flag = false;
							Map<String,Integer> key = new HashMap<String,Integer>();
							
							List<Map<String,Integer>> val = new ArrayList<Map<String,Integer>>();
							Iterator keyItr2 = mixMap.entrySet().iterator();
							while(keyItr2.hasNext()){
								Map.Entry entry = (Map.Entry) keyItr2.next(); 
							    key = (Map<String,Integer>) entry.getKey(); 
							    val = (List<Map<String,Integer>>)entry.getValue();
							    if(key.containsKey(temp)){
							    	flag = true;
							    	break;
							    }
							}
							//mixMap
							if(flag == true){
								//get value
								int value = 0;
								if(key.containsKey(temp)){
									value = key.get(temp);
								}
								//check
								boolean allhave = true;
								boolean empty = false;
								if(val.size() == 0) {
									allhave = true;
									empty = true;
								}else{
									empty = false;
									Iterator<Map<String,Integer>> it = val.iterator();
									
									while(it.hasNext()){
										
										boolean flag1 = false;
										boolean flag2 = false;
										
										Map<String,Integer>find = it.next();
										Iterator itr = find.entrySet().iterator();
										
										String key1="";
										int val1=0;
										while(itr.hasNext()){
											Map.Entry entry = (Map.Entry) itr.next(); 
										    key1 = (String)entry.getKey(); 
										    val1 = (Integer)entry.getValue(); 
										}
										//check normal
										if(val1 == 0){
											if(map1.containsKey(key1)){
												flag1 = true;
											}else{
												flag2 = true;
											}
										}else{
											if(own1.containsKey(key1)){
												num = own1.get(key1);
												if(val1 <= num)
													flag1 = true;
											}
											//check mixture
											else if(own2.containsKey(key1)){
												num = own2.get(key1);
												if(val1 <= num)
													flag2 = true;
											}
										}
										if(flag1 == false && flag2 == false){
											allhave = false;
										}
									}
								}
								//check
								if(allhave == true && empty == true && grid1 < 6){
									if(value <= gold){
										gold -= value;
										if(own2.containsKey(temp)){
											num = own2.get(temp);
											own2.put(temp, num+1);
										}else{
											own2.put(temp, 1);
										}
										grid1++;
									}
								}
								else if(allhave == true && empty == false && grid1 <= 6){
									if(value <= gold){
										gold -= value;
										if(own2.containsKey(temp)){
											num = own2.get(temp);
											own2.put(temp, num+1);
										}else{
											own2.put(temp, 1);
										}
										grid1++;
										//remove synthesis
										Iterator<Map<String,Integer>> it2 = val.iterator();
										while(it2.hasNext()){
											Map<String,Integer>find = it2.next();
											Iterator itr = find.entrySet().iterator();
											String key1="";
											int val1=0;
											while(itr.hasNext()){
												Map.Entry entry = (Map.Entry) itr.next(); 
											    key1 = (String)entry.getKey(); 
											    val1 = (Integer)entry.getValue(); 
											}
											if(own1.containsKey(key1)){
												grid1 -= val1;
												num = own1.get(key1);
												//remove
												num -= val1;
												if(num <= 0) own1.remove(key1);
												else{ 
													own1.put(key1, num);
												}
											}
											else if(own2.containsKey(key1)){
												grid1 -= val1;
												num = own2.get(key1);
												num -= val1;
												if(num <= 0) own2.remove(key1);
												else{
													own2.put(key1, num);
												}
												
											}
										}
									}
								}
							}
						}
					}
				}
				//sell
				else{
					//normal
					if(map1.containsKey(temp) && own1.containsKey(temp)){
						grid1--;
						gold += map1.get(temp);
						num = own1.get(temp);
						//remove
						if(num == 1) own1.remove(temp);
						else{ 
							num --;
							own1.put(temp, num);
						}
					}
					//mixture
					else if(own2.containsKey(temp)){
						grid1--;
						//exist
						boolean flag = false;
						Iterator rem = mixMap.entrySet().iterator();
						Map<String,Integer> key = new HashMap<String,Integer>();
						
						List<Map<String,Integer>> val = new ArrayList<Map<String,Integer>>();
						while(rem.hasNext()){
							Map.Entry entry = (Map.Entry) rem.next(); 
						    key = (Map<String,Integer>) entry.getKey(); 
						    val = (List<Map<String,Integer>>)entry.getValue();
						    if(key.containsKey(temp)){
						    	flag = true;
						    	break;
						    }
						}
						int value = 0;
						if(flag == true){
							//get value
							if(mixValue.containsKey(temp))
								gold += mixValue.get(temp);
							
							num = own2.get(temp);
							if(num == 1) own2.remove(temp);
							else{
								num --;
								own2.put(temp, num);
							}
						}
						
					}
					//consume
					else if(map4.containsKey(temp)&&own1.containsKey(temp)){
						grid1--;
						num = own1.get(temp);
						int value = map4.get(temp) * num;
						gold += value;
						own1.remove(temp);
					}
				}
			}
			System.out.println("Case "+cnt+":");
			System.out.println(gold);
			System.out.println(grid1);
			
			List<String>strList = new ArrayList<String>();
			
			Map<String,Integer> ans = new HashMap<String,Integer>();
			if(own1.size() != 0){
				Iterator res1 = own1.entrySet().iterator();
				while(res1.hasNext()){
					Map.Entry entry = (Map.Entry) res1.next(); 
				    String key = (String)entry.getKey(); 
				    Integer val = (Integer)entry.getValue(); 
				    ans.put(key, val);
				    strList.add(key);
				}
			}
			if(own2.size() != 0){
				Iterator res2 = own2.entrySet().iterator();
				while(res2.hasNext()){
					Map.Entry entry = (Map.Entry) res2.next(); 
					String key = (String)entry.getKey(); 
				    Integer val = (Integer)entry.getValue(); 
				    strList.add(key);
				    ans.put(key, val);
				}
			}
			Collections.sort(strList, new Comparator<String>() { 
		        public int compare(String a, String b) { 
		        	return a.compareTo(b); 
		        } 
		    });
			Iterator<String> it = strList.iterator();
			while(it.hasNext()){
				String resStr = it.next();
				Integer value = ans.get(resStr);
				if(map4.containsKey(resStr)){
					System.out.println(resStr+": "+value);
				}else{
					for(int j = 0 ; j < value ; j ++){
			    		System.out.println(resStr+": 1");
			    	}
				}
			}
			System.out.println();
		}
	}
	
}


你可能感兴趣的:(String,Integer,iterator,Class,import,n2)