Created
April 5, 2015 18:16
-
-
Save jones/a153972b91487e97336e to your computer and use it in GitHub Desktop.
"Incoming! Fire the defense turrets at these coordinates! Go go go!"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <vector> | |
class TurretDefense { | |
public: | |
int firstMiss(std::vector <int>xs, std::vector <int>ys, std::vector <int> times) { | |
int curx = 0; | |
int cury = 0; | |
int curtime = 0; | |
for(int i = 0; i < xs.size(); i++) { | |
if (curtime + manhattan(curx, cury, xs[i], ys[i]) <= times[i]) { | |
curtime = times[i]; | |
curx = xs[i]; | |
cury = ys[i]; | |
} else { | |
return i; | |
} | |
} | |
return -1; | |
} | |
int manhattan(int xt, int yt, int xg, int yg) { | |
return abs(xt-xg) + abs(yt-yg); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment