/* Pokemon Go Go Bruce Elenbogen */ #include #include #include #include #include using namespace std; int abs(int n){ if (n>=0) return n; return -n; } struct Stop{ int row; int col; string name; }; struct SearchNode { vector seq; int distSoFar; int bound; bool operator<(const SearchNode & rhs) const { return bound > rhs.bound; } }; vector > setUpAdj(vector locations) { vector > tempAdj(locations.size()); for (int i = 0; i < locations.size(); i++) tempAdj[i].resize(locations.size()); /*cout<<"Adjaceny Matrix\n";*/ for (int i = 0; i < locations.size(); i++) { for (int j = 0; j < locations.size(); j++) { tempAdj[i][j] = abs(locations[i].row - locations[j].row) + abs(locations[i].col - locations[j].col); //cout << tempAdj[i][j] << " "; } //cout << endl; } return tempAdj; } vector minValues(const vector > & adjMat) { vector mins(adjMat.size(), INT_MAX); for (unsigned int i = 0; i < adjMat.size(); i++) { for (unsigned int j = 0; j < adjMat.size(); j++) if (i != j && adjMat[i][j] < mins[i]) mins[i] = adjMat[i][j]; } return mins; } int computeTotalDist(const vector & location, const vector & order){ int totalDist = 0; int size = order.size(); for (int i = 0; i < size-1; i++) totalDist += abs(location[order[i+1]].row - location[order[i]].row)+ abs(location[order[i+1]].col - location[order[i]].col); totalDist += abs(location[order[0]].row - location[order[size-1]].row)+ abs(location[order[0]].col - location[order[size-1]].col); return totalDist; } int findIndex(int index, const vector & uniqueNames){ for (int i = 0; i < uniqueNames.size(); i++) if (index == uniqueNames[i]) return i; return -1; } bool nextPermute(vector & lastPermute, vector > &uniqueNames ){ bool done = false; int index; for(int i = 1 ; i < lastPermute.size()&& !done;i++){ index = findIndex(lastPermute[i], uniqueNames[i]); //if (index < 0) // system("pause"); if (index & locations, vector > & indexUniqueNames ){ vector temp(1,0); bool placed; string name; indexUniqueNames.push_back(temp); for (int i = 1; i < locations.size(); i++){ name = locations[i].name; placed= false; for (int j = 0; j < indexUniqueNames.size()&& !placed; j++) if (name == locations[indexUniqueNames[j][0]].name){ placed = true; indexUniqueNames[j].push_back(i); } if (!placed){ temp[0]=i; indexUniqueNames.push_back(temp); } } } int ComputeBound(vector seq, vector minDists, vector permute, int bndSoFar) { for (int i = 1; i < permute.size(); i++) if (findIndex(permute[i], seq) < 0 ) bndSoFar += minDists[permute[i]]; return bndSoFar; } int branchNBound(const vector > & adjMat, const vector & locations, const vector & permute, const vector minDists) { SearchNode node; SearchNode newNode; priority_queue que; node.seq.push_back(0); node.distSoFar = 0; node.bound = ComputeBound(node.seq, minDists, permute, 0) + minDists[0]; que.push(node); while (!que.empty()) { node = que.top(); que.pop(); if (node.seq.size() > permute.size() ) { return node.distSoFar; } else if (node.seq.size() == permute.size()) { node.seq.push_back(0); node.distSoFar += (adjMat[node.seq[node.seq.size() - 2]][0]); node.bound = node.distSoFar ; //return node.distSoFar; que.push(node); } else { for (int i = 1; i < permute.size(); i++) { if (findIndex(permute[i], node.seq) < 0) { newNode.seq = node.seq; newNode.seq.push_back(permute[i]); newNode.distSoFar = node.distSoFar + (adjMat[permute[i]][node.seq[node.seq.size() - 1]]); newNode.bound = ComputeBound(newNode.seq, minDists, permute, newNode.distSoFar) + minDists[0]; que.push(newNode); } }//for }//else }//while } int main(){ int numStops; vector locations; vector permute; vector temp; vector bestOrder; vector > uniqueNames; vector > adjMat; vector minDist; vector seq; int bestDist; int dist; int curBnd; cin>> numStops; locations.resize(numStops+1); adjMat.resize(numStops + 1); for (int i = 0; i < numStops+1; i++) adjMat[i].resize(numStops + 1); locations[0].row = 0; locations[0].col = 0; locations[0].name=""; for (int i = 1; i < numStops+1; i++) cin>> locations[i].row>>locations[i].col>>locations[i].name; setUpUniqueNames(locations,uniqueNames ); adjMat = setUpAdj(locations); minDist = minValues(adjMat); //cout<<"after set "<