示例#1
0
void Router::backtrack(Coordinate *target, Coordinate *source){
  Coordinate *current = target;
  GridPoint currentGridPoint;
  GridPoint neighborGridPoint;
  do{
    currentGridPoint = getGridPointAt(current);
    //mark the route id
    markRoute(current);
    //find the next node

    Coordinate *neighbors = current->GetNeighbors();
    int i;
    //if any of them is the target, stop,
    for (i = 0; i < 4; i++) {
      if(!neighbors[i].InBound(rows, cols)){
        continue;
      }
      neighborGridPoint = getGridPointAt(&neighbors[i]);

      if((neighborGridPoint.StepId < currentGridPoint.StepId && neighborGridPoint.StepId != 0) || neighbors[i].Equals(source)){
        current = &neighbors[i];
        break;
      }
    }
    //repeat until is source reached
  }while(!current->Equals(source));
  //mark the source
  markRoute(source);
}
示例#2
0
void Router::propagate(Coordinate *source, Coordinate *target){
  int stepId = 0;
  queue->Add(source);
  visit(source, stepId);
  Coordinate *current;
  GridPoint currentGridPoint;
  bool targetFound = false;

  do{
    //get the next node from queue
    current = queue->Remove();
    currentGridPoint = getGridPointAt(current);
    stepId = currentGridPoint.StepId + 1;
    //get neighbors of source
    Coordinate *neighbors = current->GetNeighbors();

    int i;
    //if any of them is the target, stop,
    for (i = 0; i < 4; i++) {
      if(neighbors[i].Equals(target)){
        visit(&neighbors[i], stepId);
        targetFound = true;
        break;
      }

      //add it to the queue if 
      //it is not out of bounds
      //it is not visited
      if(neighbors[i].InBound(rows, cols) && !isVisited(&neighbors[i])){
        queue->Add(&neighbors[i]);
        visit(&neighbors[i], stepId);
      }

    }
  }while(!queue->IsEmpty() && !targetFound);

  //else add them to the queue
  //do the same with the rest of the elements in the queue until the target is
  //reached

}