题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
java 代码
<!--package F20160903;-->
import java.util.*;
public class Main{
public static List<Role> p1 = new ArrayList<Role>();
public static List<Role> p2 = new ArrayList<Role>();
public static List<Role> firstAttack;
public static List<Role> lastAttack;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
firstAttack = p1;
lastAttack = p2;
Role hero1 = new Role(0,30);
Role hero2 = new Role(0,30);
p1.add(hero1);
p2.add(hero2);
int n = sc.nextInt();
sc.nextLine();
String temp;
String[] cmd;
for(int i=0;i<n;i++) {
temp = sc.nextLine()+" ";
cmd = temp.split(" ");
//召唤随从
if(cmd.length==4) {
action(cmd[0],Integer.parseInt(cmd[1]),
Integer.parseInt(cmd[2]),Integer.parseInt(cmd[3]));
}
//攻击
else if(cmd.length==3) {
action(cmd[0],Integer.parseInt(cmd[1]),
Integer.parseInt(cmd[2]),0);
}
//回合结束
else {
action(cmd[0],0,0,0);
}
}
//判断胜负
if(p1.get(0).health<=0) {
System.out.println(-1);
}
else {
if(p2.get(0).health>0) System.out.println(0);
else System.out.println(1);
}
result(p1);
result(p2);
sc.close();
}
//判断执行动作
public static void action(String operate,int a,int b,int c) {
if(operate.equals("summon")) {
firstAttack.add(a,new Role(b,c));
}
else if(operate.equals("attack")) {
firstAttack.get(a).health -= lastAttack.get(b).attack;
lastAttack.get(b).health -= firstAttack.get(a).attack;
dieOfRetinue();
}
//回合结束交换先后手
else {
List<Role> t;
t = firstAttack;
firstAttack = lastAttack;
lastAttack = t;
}
}
//检测是否有出局随从
public static void dieOfRetinue() {
for(int i=1;i<p1.size();i++) {
if(p1.get(i).health<=0) {
p1.remove(i);
}
}
for(int i=1;i<p2.size();i++) {
if(p2.get(i).health<=0) {
p2.remove(i);
}
}
}
//输出结果
public static void result(List<Role> L) {
System.out.println(L.get(0).health);
System.out.print(L.size()-1+" ");
if(L.size()>1) {
for(int i=1;i<L.size();i++) {
System.out.print(L.get(i).health+" ");
}
}
System.out.println();
}
}
//英雄类
class Role{
int health;
int attack;
public Role(int a,int h) {
health = h;
attack = a;
}
}