試題說明
筆試題只有一道,限時1小時。
模擬一個戰爭外交游戲,游戲中定義了三種操作:
A city1 Hold : 軍隊A 占領了city1
A city1 Move city2 : 軍隊A從city1移動到city2
A city1 Support B : 占領了city1的軍隊A增援軍隊B,得到增援的軍隊B的strength+1;
注意:
1. 每支軍隊的初始strength為1;
2. 如果有多個軍隊到達了同一個城市,則strength高的軍隊存活並占領該城市,strength低軍隊死亡;
3. 如果某支軍隊正在交戰(即其所在的城市除自己外還有其他軍隊),則該軍隊的任何增援行動無效;
輸入:
一系列字符串表示的的Action,如:
A Munich Hold
B Bohemia Move Munich
C Warsaw Support B
輸出:
按字母表順序排列的各個軍隊的狀態(死亡或者存活,如果存活輸出駐扎地點),上面用例的輸出為:
A [dead]
B Munich
C Warsaw
下面是原題:
JAVA題解:
1 //游戲的規則是把所有的動作執行之后再判斷狀態,每個回合之中的所有動作沒有先后之分。 2 import java.util.*; 3 4 public class testAirBnB { 5 static List<String> evaluateActions(List<String> actions) { 6 Map<String, army> armies = new TreeMap(); 7 Map<String, city> cities = new HashMap<>(); 8 ArrayList<String[]> manu = new ArrayList<>(); 9 ArrayList<String[]> supportList = new ArrayList<>(); 10 11 for (String action : actions) { 12 //將每個aciton拆解 13 String[] s = action.split(" "); 14 //注冊每個軍隊 15 army a = new army(s[0]); 16 armies.put(s[0], a); 17 manu.add(s); 18 } 19 20 for (String[] str : manu) { 21 //遍歷每個action, 執行每個action 22 //獲取當前action的執行主體軍隊 23 army a = armies.get(str[0]); 24 //記錄該軍隊當前所在城市 25 a.whereNow = str[1]; 26 //保存目前軍隊所在城市 27 city tmpCity; 28 29 //為a當前所在的城市注冊,如果記錄中不存在該城市,新建一個記錄,並將a軍隊加入其hold軍隊表 30 if (!cities.containsKey(a.whereNow)) {//該城市之前未被注冊過 31 tmpCity = new city(a.whereNow, a.strength, a.name); 32 cities.put(a.whereNow, tmpCity); 33 } else { 34 //該城市之前注冊過,但因為之前的戰斗所有軍隊都死亡,現在可以重新hold;這里假設 35 //所有的操作都是有效的,即不存在向已有軍隊駐扎的城市hold的action發生 36 tmpCity = cities.get(a.whereNow); 37 tmpCity.holdArmies.put(a.name, a.strength); 38 } 39 40 if (str[2].equals("Hold")) { 41 //keep default; 42 } else if (str[2].equals("Move")) { 43 //從當前的城市撤退 44 tmpCity.holdArmies.remove(a.name); 45 //進軍到目的城市 46 a.whereNow = str[3]; 47 //如果要去的城市已經被注冊,直接加入駐扎名單 48 if (cities.containsKey(a.whereNow)) { 49 tmpCity = cities.get(a.whereNow); 50 tmpCity.holdArmies.put(a.name, a.strength); 51 } 52 //否則說明要去的城市未被駐扎過,直接注冊該城市並記錄駐扎信息 53 else { 54 tmpCity = new city(a.whereNow, a.strength, a.name); 55 cities.put(a.whereNow, tmpCity); 56 } 57 58 59 } else if (str[2].equals("Support")) { 60 //必須等到所有的軍隊移動完畢才能確定是否可以增援 61 supportList.add(str); 62 // //為友軍加buf 63 // army friend = armies.get(str[3]); 64 // friend.strength++; 65 // //更新友軍所在城市注冊表中友軍的strength 66 // city friendCity = cities.get(friend.whereNow); 67 // friendCity.holdArmies.put(friend.name, friend.strength); 68 } 69 } 70 71 //處理增援 72 //如果A沒有underAttack, 則可以增援 73 //否則不可以增援 74 for(String[] sptAction: supportList){ 75 //如果A所在城市的駐扎軍隊大於1只,說明A正在underAttack, A的增援行動無效 76 77 if(cities.get(armies.get(sptAction[0]).whereNow).holdArmies.size()>1) 78 continue; 79 //否則增援有效 80 else{ 81 //為友軍加buf 82 army friend = armies.get(sptAction[3]); 83 friend.strength++; 84 //更新友軍所在城市注冊表中友軍的strength 85 city friendCity = cities.get(friend.whereNow); 86 friendCity.holdArmies.put(friend.name, friend.strength); 87 } 88 } 89 90 //跟據城市來判斷每只軍隊的生存狀況 91 for (city tmpCity : cities.values()) { 92 //當前城市的駐扎軍隊少於兩支,不會發生戰爭 93 if (tmpCity.holdArmies.size() < 2) 94 continue; 95 //駐扎軍隊大於兩支 96 97 else { 98 List<Map.Entry<String, Integer>> list = new ArrayList<>(tmpCity.holdArmies.entrySet()); 99 // 按treeMap的值從大到小排序 100 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { 101 @Override 102 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 103 return -o1.getValue().compareTo(o2.getValue()); 104 } 105 }); 106 //如果該城市的駐扎軍隊表中戰力靠前的兩支軍隊的戰力相等,則駐扎在該城市的所有軍隊都回死亡(包括戰力更低的) 107 if (list.get(0).getValue().equals(list.get(1).getValue())) { 108 //該城市所有軍隊都死亡 109 for (int i = 0; i < list.size(); i++) { 110 armies.get(list.get(i).getKey()).isAlive = false; 111 } 112 } else { 113 //否則除了第一支軍隊(戰力最高的軍隊),其余軍隊死亡 114 for (int i = 1; i < list.size(); i++) { 115 armies.get(list.get(i).getKey()).isAlive = false; 116 117 } 118 } 119 } 120 } 121 122 // 循環檢查每個城市的狀態,如果isAlive為false, 輸出城市名+[dead] 123 // 否則輸出城市名+whereNow 124 List<String> result = new ArrayList<>(); 125 for (army unit : armies.values()) { 126 //如果狀態標記為dead,直接輸出 127 if (!unit.isAlive) 128 result.add(unit.name + " " + "[dead]"); 129 else { 130 result.add(unit.name + " " + unit.whereNow); 131 } 132 } 133 134 for (String s : result) 135 System.out.println(s); 136 137 return result; 138 } 139 140 public static void main(String[] args) { 141 142 List<String> input = new ArrayList<>(); 143 //測試數據1 144 // input.add("A Munich Hold");input.add("B Warsaw Move Bohemia"); 145 //測試數據2 146 input.add("A Munich Hold");input.add("B Bohemia Move Munich");input.add("C Warsaw Support B"); 147 //測試數據3 148 // input.add("A Munich Hold");input.add("B Bohemia Move Munich");input.add("C Prussia Move Munich");input.add("D Warsaw Hold"); 149 //測試數據4 150 // input.add("A Munich Support B");input.add("B Bohemia Move Prussia");input.add("C Prussia Hold");input.add("D Warsaw Move Munich"); 151 //測試數據5 152 // input.add("A Munich Support B");input.add("B Oakland Move Munich"); 153 evaluateActions(input); 154 } 155 156 static class army { 157 String name; 158 String whereNow; 159 int strength = 1; 160 // boolean underAttack = false; 161 boolean isAlive = true; 162 163 army(String _name) { 164 name = _name; 165 } 166 } 167 168 static class city { 169 String name; 170 TreeMap<String, Integer> holdArmies = new TreeMap<>(); 171 172 city(String city_name, int strength, String army_name) { 173 name = city_name; 174 holdArmies.put(army_name, strength); 175 } 176 } 177 178 }