コード例 #1
0
ファイル: solution.cpp プロジェクト: Time1ess/MyCodes
 int openLock(vector<string> &deadends, string &target) {
     // Write your code here
     unordered_set<string> visited, deads;
     for(auto deadend: deadends)
         deads.insert(deadend);
     if(deads.find("0000") != deads.end())
         return -1;
     queue<pair<string, int>> combs;
     combs.push({"0000", 0});
     visited.insert("0000");
     string next;
     pair<string, int> current;
     while(!combs.empty())
     {
         current = combs.front();
         combs.pop();
         if(current.first == target)
             break;
         for(int i = 0; i < 4; i++)
         {
             next = rotate(current.first, i, true);
             push_next(visited, deads, combs, next, current.second + 1);
             next = rotate(current.first, i, false);
             push_next(visited, deads, combs, next, current.second + 1);
         }
     }
     if(current.first == target)
         return current.second;
     return -1;
 }
コード例 #2
0
ファイル: Fl_Help_Dialog.cpp プロジェクト: edeproject/svn
void Fl_Help_Dialog::previous_url()
{
    char *url = pop_prev();
    if(url) {
        no_prevpush = true;
        push_next(htmlWidget->filename());

        htmlWidget->load(url);
        label(htmlWidget->title());
        delete []url;
        no_prevpush = false;
    }
}
コード例 #3
0
ファイル: navfn.cpp プロジェクト: AutoPark/Nav_Stack
  inline void
    NavFn::updateCellAstar(int n)
    {
      // get neighbors
      float u,d,l,r;
      l = potarr[n-1];
      r = potarr[n+1];		
      u = potarr[n-nx];
      d = potarr[n+nx];
      //ROS_INFO("[Update] c: %0.1f  l: %0.1f  r: %0.1f  u: %0.1f  d: %0.1f\n", 
      //	 potarr[n], l, r, u, d);
      // ROS_INFO("[Update] cost of %d: %d\n", n, costarr[n]);

      // find lowest, and its lowest neighbor
      float ta, tc;
      if (l<r) tc=l; else tc=r;
      if (u<d) ta=u; else ta=d;

      // do planar wave update
      if (costarr[n] < COST_OBS)	// don't propagate into obstacles
      {
        float hf = (float)costarr[n]; // traversability factor
        float dc = tc-ta;		// relative cost between ta,tc
        if (dc < 0) 		// ta is lowest
        {
          dc = -dc;
          ta = tc;
        }

        // calculate new potential
        float pot;
        if (dc >= hf)		// if too large, use ta-only update
          pot = ta+hf;
        else			// two-neighbor interpolation update
        {
          // use quadratic approximation
          // might speed this up through table lookup, but still have to 
          //   do the divide
          float d = dc/hf;
          float v = -0.2301*d*d + 0.5307*d + 0.7040;
          pot = ta + hf*v;
        }

        //ROS_INFO("[Update] new pot: %d\n", costarr[n]);

        // now add affected neighbors to priority blocks
        if (pot < potarr[n])
        {
          float le = INVSQRT2*(float)costarr[n-1];
          float re = INVSQRT2*(float)costarr[n+1];
          float ue = INVSQRT2*(float)costarr[n-nx];
          float de = INVSQRT2*(float)costarr[n+nx];

          // calculate distance
          int x = n%nx;
          int y = n/nx;
          float dist = (x-start[0])*(x-start[0]) + (y-start[1])*(y-start[1]);
          dist = sqrtf(dist)*(float)COST_NEUTRAL;

          potarr[n] = pot;
          pot += dist;
          if (pot < curT)	// low-cost buffer block 
          {
            if (l > pot+le) push_next(n-1);
            if (r > pot+re) push_next(n+1);
            if (u > pot+ue) push_next(n-nx);
            if (d > pot+de) push_next(n+nx);
          }
          else
          {
            if (l > pot+le) push_over(n-1);
            if (r > pot+re) push_over(n+1);
            if (u > pot+ue) push_over(n-nx);
            if (d > pot+de) push_over(n+nx);
          }
        }

      }

    }