中级农民
- 积分
- 217
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-11-30
- 最后登录
- 1970-1-1
|
想了想,觉得没必要藏着掖着代码,一道题而已,欢迎大家讨论!
代码可以直接在terminal编译通过,欢迎下载测试
- /*
- We are looking at a global minimum case here. A person might not get the closest bike for him, but the sum of all person-bike pair is minimized.
- Let’s use a heap to solve this problem.
- Assuming that we can walk pass a person or a bike, then the person-bike distance is simply the Manhattan Distance given that the whole map has a grid representation.
- This is a greedy approach, and it makes sense because a one match replaces only on other match, and a closer match is always better than an further match.
- */
- #include <vector>
- #include <queue>
- #include <utility>
- #include <iostream>
- #include <unordered_set>
- using namespace std;
- vector< pair<pair<int, int>, pair<int, int>> > MatchBike(const vector<string> & grid);
- string Encode(const pair<int, int>& pos);
- pair<int, int> Decode(const string& pos);
- int Dist(const pair<int, int>& pos1, const pair<int, int>& pos2);
- void PrintMatch(const vector< pair<pair<int, int>, pair<int, int>> >& match);
- struct CompMatch{
- bool operator()(const pair<pair<int, int>, pair<int, int>>& match1,
- const pair<pair<int, int>, pair<int, int>>& match2){
- return ( Dist(match1.first, match1.second) >
- Dist(match2.first, match2.second) );
- }
- };
- int main(int argc, char const *argv[])
- {
-
- vector<string> grid =
- {
- "B..P.",
- "..B..",
- "P....",
- "....P",
- "....B",
- };
- PrintMatch(MatchBike(grid));
- return 0;
- }
- vector< pair<pair<int, int>, pair<int, int>> > MatchBike(const vector<string> & grid){
- /* 0. MISC */
- /* 1. prep */
- vector< pair<pair<int, int>, pair<int, int>> > answer;
- unordered_set<string> people, bikes;
- for(unsigned int i = 0; i < grid.size(); ++i){
- for(unsigned int j = 0; j < grid[i].size(); ++j){
- if(grid[i][j] == 'B')bikes.emplace(move(Encode({i, j})));
- if(grid[i][j] == 'P')people.emplace(move(Encode({i, j})));
- }
- }
- priority_queue<pair<pair<int, int>, pair<int, int>>,
- vector<pair<pair<int, int>, pair<int, int>>>,
- CompMatch > matchPq;
-
- for(auto pItr = people.begin(); pItr != people.end(); ++pItr){
- for(auto bItr = bikes.begin(); bItr != bikes.end(); ++bItr){
- matchPq.emplace(move(Decode(*pItr)), move(Decode(*bItr)));
- }
- }
-
- /* 2. key algo */
- while(!people.empty()){
- auto match = matchPq.top(); matchPq.pop();
- string personStr = Encode(match.first);
- string bikeStr = Encode(match.second);
- if(bikes.find(bikeStr) == bikes.end() ||
- people.find(personStr) == people.end() )continue;
- bikes.erase(bikeStr);
- people.erase(personStr);
- answer.emplace_back(match);
- }
- /* 3. answer */
- return answer;
- }
- string Encode(const pair<int, int>& pos){
- return to_string(pos.first) + " " + to_string(pos.second);
- }
- pair<int, int> Decode(const string& pos){
- int mid = pos.find(" ");
- int first = stoi(pos.substr(0, mid)), second = stoi(pos.substr(mid + 1));
- return {first, second};
- }
- int Dist(const pair<int, int>& pos1, const pair<int, int>& pos2){
- return abs(pos1.first - pos2.first) + abs(pos1.second - pos2.second);
- }
- void PrintMatch(const vector< pair<pair<int, int>, pair<int, int>> >& match){
- for(auto m: match){
- cout << "person: (" << m.first.first << ", " << m.first.second << ") ";
- cout << "bike: (" << m.second.first << ", " << m.second.second << ")" << endl;
- }
- }
复制代码 |
|