Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.22 KB | None | 0 0
  1. class TreePos
  2. {
  3.     private:
  4.         int treeHeight;
  5.         int xcord;
  6.         int ycord;
  7.    
  8.     public:
  9.     TreePos(int treeHeight, int x, int y)
  10.     {
  11.         this->treeHeight = treeHeight;
  12.         this->xcord = x;
  13.         this->ycord = y;
  14.     }
  15.    
  16.     int TreeHeight()
  17.     {
  18.         return this->treeHeight;
  19.     }
  20.    
  21.     int XCord()
  22.     {
  23.         return this->xcord;
  24.     }
  25.    
  26.     int YCord()
  27.     {
  28.         return this->ycord;
  29.     }
  30. };
  31.  
  32. class HeapRepr
  33. {
  34.   public:
  35.     int distanceCovered;
  36.     int estimatedDistanceToGoal;
  37.     pair<int,int> currentPosition;
  38.     HeapRepr(int distanceCovered, int estimatedDistanceToGoal,pair<int,int> & currentPosition )
  39.     {
  40.         this->distanceCovered = distanceCovered;
  41.         this->estimatedDistanceToGoal = estimatedDistanceToGoal;
  42.         this->currentPosition = currentPosition;
  43.     }
  44.    
  45.     HeapRepr(const HeapRepr &other)
  46.     {
  47.         this->distanceCovered = other.distanceCovered;
  48.         this->estimatedDistanceToGoal = other.estimatedDistanceToGoal;
  49.         this->currentPosition = other.currentPosition;
  50.     }
  51. };
  52.  
  53. bool operator > (const HeapRepr a, const HeapRepr b)
  54. {
  55.     return a.distanceCovered + a.estimatedDistanceToGoal > b.distanceCovered + b.estimatedDistanceToGoal;
  56. }
  57.  
  58. class Solution {
  59. public:
  60.     int columns;
  61.    
  62.     static bool TreePosCompare(TreePos &a, TreePos &b)
  63.     {
  64.       return a.TreeHeight() < b.TreeHeight();
  65.     }
  66.    
  67.     int CalculateEstimatedDistance(pair<int, int> &src, pair<int, int> &dst)
  68.     {
  69.         return abs(src.first - dst.first) + abs(src.second - dst.second);
  70.     }
  71.    
  72.     bool isPassable(vector<vector<int>> & forest, int i, int j)
  73.     {
  74.         if(i<0 || j<0 || i>= forest.size() || j>=forest[0].size())
  75.         {
  76.             return false;
  77.         }
  78.         if(forest[i][j] == 0)
  79.         {
  80.             return false;
  81.         }
  82.         return true;
  83.     }
  84.    
  85.     int AStar(
  86.         vector<vector<int>> forest,
  87.         pair<int, int> &src,
  88.         pair<int, int> &dst)
  89.     {
  90.         priority_queue<HeapRepr, vector<HeapRepr>,greater<vector<HeapRepr>::value_type>> heap;
  91.         unordered_set<int> visited;
  92.        
  93.         heap.push(HeapRepr(0, CalculateEstimatedDistance(src, dst), src));
  94.         vector<pair<int, int>> directions =
  95.         {
  96.             make_pair(0,-1),
  97.             make_pair(-1,0),
  98.             make_pair(1,0),
  99.             make_pair(0,1),
  100.         };
  101.        
  102.         while(!heap.empty())
  103.         {
  104.             auto current = heap.top();
  105.             heap.pop();
  106.            
  107.             // IF we comment out this section and uncomment "Section 1" then the code fails
  108.             int currentOneD = OneDProject(current.currentPosition.first, current.currentPosition.second);
  109.             if (visited.count(currentOneD)) continue;
  110.             visited.insert(currentOneD);
  111.            
  112.             if(current.currentPosition.first == dst.first && current.currentPosition.second == dst.second)
  113.             {
  114.                 return current.distanceCovered;
  115.             }
  116.            
  117.             for(int i =0;i<4;i++)
  118.             {
  119.                 HeapRepr next = HeapRepr(current);
  120.                 next.currentPosition.first += directions[i].first;
  121.                 next.currentPosition.second += directions[i].second;
  122.                 int nextOneD = OneDProject(next.currentPosition.first, next.currentPosition.second);
  123.                
  124.                 if(visited.count(nextOneD) == 0 &&
  125.                   isPassable(
  126.                       forest,
  127.                       next.currentPosition.first,
  128.                       next.currentPosition.second))
  129.                 {
  130.                     // Section 1
  131.                     // visited.insert(nextOneD);
  132.                     next.distanceCovered += 1;
  133.                     next.estimatedDistanceToGoal = CalculateEstimatedDistance(next.currentPosition, dst);
  134.                     heap.push(next);
  135.                 }
  136.             }
  137.            
  138.         }
  139.         return -1;
  140.     }
  141.    
  142.     int OneDProject(int i, int j)
  143.     {
  144.         return i*columns + j;
  145.     }
  146.    
  147.     pair<int, int> TwoDProject(int val)
  148.     {
  149.         int quotient = val/columns;
  150.         return make_pair(quotient, val - (quotient*columns) );
  151.     }
  152.    
  153.     int cutOffTree(vector<vector<int>>& forest) {
  154.         columns = forest[0].size();
  155.         vector<TreePos> trees;
  156.        
  157.         // Setup
  158.         for(int i=0;i<forest.size();i++)
  159.         {
  160.             for (int j=0;j<forest[0].size();j++)
  161.             {  
  162.                 if (forest[i][j] > 1)
  163.                 {
  164.                     trees.push_back(TreePos(forest[i][j], i, j));
  165.                 }
  166.             }
  167.         }
  168.        
  169.         sort(trees.begin(), trees.end(), TreePosCompare);
  170.        
  171.         auto src = make_pair(0,0);
  172.         int steps = 0;
  173.         for (auto it=trees.begin(); it!=trees.end(); it++)
  174.         {
  175.             auto dst = make_pair(it->XCord(), it->YCord());
  176.             int distanceTraveled = AStar(forest, src, dst);
  177.            
  178.             if (distanceTraveled<0)
  179.             {
  180.                 return -1;
  181.             }
  182.            
  183.             swap(src, dst);
  184.             steps+=distanceTraveled;
  185.         }
  186.        
  187.         return steps;
  188.        
  189.     }
  190. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement