Skip to content

Commit

Permalink
Zombie Smasher implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Xia committed Feb 14, 2014
1 parent da2c0f0 commit f3af286
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>fun</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
369 changes: 369 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .settings/org.eclipse.jdt.ui.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
formatter_profile=org.eclipse.jdt.ui.default.sun_profile
formatter_settings_version=12
16 changes: 16 additions & 0 deletions resources/zombie_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
3
4
1 0 0
-1 0 0
10 10 1000
10 -10 1000
3
1 1 0
2 2 0
3 3 0
5
10 10 1000
-10 10 1000
10 -10 1000
-10 10 1000
20 20 2000
117 changes: 117 additions & 0 deletions src/com/mxia/personal/ZombieSmasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.mxia.personal;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.StringTokenizer;

public class ZombieSmasher {

private static final int TIME_ZOMBIE_LIFE = 1000;
private static final int TIME_MOVE_ADJ = 100;
private static final int TIME_RECHARGE = 750;

public static void main(String[] args) {
BufferedReader in = null;
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(
"resources/zombie_input.txt")));
String line = in.readLine();
if (line != null) {
int cases = Integer.parseInt(line);
for (int i = 1; i <= cases; ++i) {
solve(i, in, out);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
out.close();
}
}

private static void solve(int caseNumber, BufferedReader in, PrintWriter out)
throws IOException, NumberFormatException {
int zCount = Integer.parseInt(in.readLine());
int[] x = new int[zCount];
int[] y = new int[zCount];
int[] time = new int[zCount];
StringTokenizer st;
for (int i = 0; i < zCount; ++i) {
st = new StringTokenizer(in.readLine());
x[i] = Integer.parseInt(st.nextToken());
y[i] = Integer.parseInt(st.nextToken());
time[i] = Integer.parseInt(st.nextToken());
}

int[][] minTime = new int[zCount][zCount];

for (int i = 0; i < zCount; ++i) {
for (int j = 0; j < zCount; ++j) {
minTime[i][j] = Integer.MAX_VALUE;
}
}

for (int i = 0; i < zCount; ++i) {
// compute the time needed to move from (0, 0) to (x[i], y[i])
int moveTime = Math.max(Math.abs(x[i]), Math.abs(y[i]))
* TIME_MOVE_ADJ;
if (moveTime <= time[i] + TIME_ZOMBIE_LIFE) {
// could get there in time
minTime[i][0] = Math.max(moveTime, time[i]);
}
}

for (int index1 = 0; index1 < zCount; ++index1) {
for (int index2 = 0; index2 < zCount; ++index2) {
if (index2 == index1) {
continue;
}
// time to get from (x[i], y[i]) to (x[j], y[j])
int moveTime = Math.max(
TIME_RECHARGE,
Math.max(Math.abs(x[index1] - x[index2]),
Math.abs(y[index1] - y[index2]))
* TIME_MOVE_ADJ);
for (int j = 0; j < zCount; ++j) {
if (minTime[index1][j] == Integer.MAX_VALUE) {
continue;
}
int timeToNextZ = minTime[index1][j] + moveTime;
if (timeToNextZ < minTime[index2][j + 1]
&& timeToNextZ <= time[index2] + TIME_ZOMBIE_LIFE) {
minTime[index2][j + 1] = Math.max(timeToNextZ,
time[index2]);
}
}
}
}

// matrix is built; now find how many columns (minTime[][j]) have non
// default values, indicating z[j] is reachable
int count = 0;
for (int j = 0; j < zCount; ++j) {
for (int i = 0; i < zCount; ++i) {
if (minTime[i][j] != Integer.MAX_VALUE) {
count++;
break;
}
}
}

System.out.println(MessageFormat.format("Case #{0}: {1}", caseNumber,
count));
}
}

0 comments on commit f3af286

Please sign in to comment.