Advertisement
Singasking

Untitled

Jul 23rd, 2023
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. int Solution::solve(vector<vector<int> > &A) {
  2.     priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> q;
  3.    
  4.     q.push({A[0][0],{0,0}});
  5.      q.push({A[0][1],{1,0}});
  6.       q.push({A[0][2],{2,0}});
  7.     vector<int> hPaint(A.size(),-1);
  8.     vector<int> hCost(A.size(),1e7);
  9.     while(!q.empty()) {
  10.         auto element = q.top();
  11.         q.pop();
  12.         int cost=element.first;
  13.         int color = element.second.first;
  14.         int no = element.second.second;
  15.         if(no==A.size()-1) {
  16.              if(hPaint[no-1]!=color) return cost;
  17.         }
  18.         if(cost<hCost[no]) {
  19.             if(no==0 || (hPaint[no-1]!=color)) {
  20.             hCost[no] = cost;
  21.             hPaint[no] = color;
  22.             } else continue;
  23.         } else { continue; }
  24.        
  25.        
  26.            
  27.      q.push({A[no+1][1]+cost,{1,no+1}});
  28.       q.push({A[no+1][2]+cost,{2,no+1}});
  29.        q.push({A[no+1][0]+cost,{0,no+1}});
  30.        
  31.    
  32. }
  33. return 1;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement