AirBnB春招笔试题

试题说明

笔试题只有一道,限时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 }
TALK IS CHEAP, SHOW ME THE CODE
原文地址:https://www.cnblogs.com/greatLong/p/10676209.html