Digging Invaders: a bit faster.

develop
expwnent 2012-12-17 20:44:35 -05:00
parent 937769874d
commit 110a9a5579
1 changed files with 15 additions and 26 deletions

@ -26,6 +26,7 @@
#include "df/world.h" #include "df/world.h"
#include <algorithm> #include <algorithm>
#include <ctime>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
@ -195,7 +196,6 @@ command_result diggingInvadersFunc(color_ostream& out, std::vector<std::string>&
void doDiggingInvaders(color_ostream& out, void* ptr) { void doDiggingInvaders(color_ostream& out, void* ptr) {
CoreSuspender suspend; CoreSuspender suspend;
map<df::coord, set<Edge> > edgeSet;
set<df::coord> invaderPts; set<df::coord> invaderPts;
set<df::coord> localPts; set<df::coord> localPts;
map<df::coord, df::coord> parentMap; map<df::coord, df::coord> parentMap;
@ -228,19 +228,13 @@ void doDiggingInvaders(color_ostream& out, void* ptr) {
} else { } else {
continue; continue;
} }
vector<Edge>* neighbors = getEdgeSet(out, unit->pos, cache, xMax, yMax, zMax);
set<Edge>& rootEdges = edgeSet[unit->pos];
for ( auto i = neighbors->begin(); i != neighbors->end(); i++ ) {
Edge edge = *i;
rootEdges.insert(edge);
}
delete neighbors;
} }
int32_t localPtsFound = 0; int32_t localPtsFound = 0;
set<df::coord> closedSet; set<df::coord> closedSet;
clock_t t0 = clock();
clock_t totalEdgeTime = 0;
while(!fringe.empty()) { while(!fringe.empty()) {
df::coord pt = *(fringe.begin()); df::coord pt = *(fringe.begin());
fringe.erase(fringe.begin()); fringe.erase(fringe.begin());
@ -249,30 +243,23 @@ void doDiggingInvaders(color_ostream& out, void* ptr) {
out.print("Double closure! Bad!\n"); out.print("Double closure! Bad!\n");
break; break;
} }
closedSet.insert(pt); //closedSet.insert(pt);
if ( localPts.find(pt) != localPts.end() ) { if ( localPts.find(pt) != localPts.end() ) {
localPtsFound++; localPtsFound++;
if ( localPtsFound >= localPts.size() ) if ( localPtsFound >= localPts.size() )
break; break;
if ( costMap[pt].cost[1] > 0 ) if ( costMap[pt].cost[1] > 0 || costMap[pt].cost[2] > 0 || costMap[pt].cost[3] > 0 )
break; break;
} }
if ( edgeSet.find(pt) == edgeSet.end() ) { Cost& myCost = costMap[pt];
set<Edge>& temp = edgeSet[pt]; clock_t edgeTime = clock();
vector<Edge>* edges = getEdgeSet(out, pt, cache, xMax, yMax, zMax); vector<Edge>* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax);
for ( auto a = edges->begin(); a != edges->end(); a++ ) { totalEdgeTime += (clock() - edgeTime);
Edge e = *a; for ( auto a = myEdges->begin(); a != myEdges->end(); a++ ) {
temp.insert(e); Edge &e = *a;
} df::coord& other = e.p1;
delete edges;
}
Cost myCost = costMap[pt];
set<Edge>& myEdges = edgeSet[pt];
for ( auto a = myEdges.begin(); a != myEdges.end(); a++ ) {
Edge e = *a;
df::coord other = e.p1;
if ( other == pt ) if ( other == pt )
other = e.p2; other = e.p2;
if ( costMap.find(other) == costMap.end() || costMap[other] > myCost + e.cost ) { if ( costMap.find(other) == costMap.end() || costMap[other] > myCost + e.cost ) {
@ -282,8 +269,10 @@ void doDiggingInvaders(color_ostream& out, void* ptr) {
parentMap[other] = pt; parentMap[other] = pt;
} }
} }
edgeSet.erase(pt); delete myEdges;
} }
clock_t time = clock() - t0;
out.print("time = %d, totalEdgeTime = %d\n", time, totalEdgeTime);
//find important edges //find important edges
list<Edge> importantEdges; list<Edge> importantEdges;