示例#1
0
 void dfs(vector<vector<int> > &vvi, const int depth, const int n) {
     if(depth == n) {
         ++ans;
         return;
     }
     for(int i = 0; i < n; ++i) {
         if(vvi[depth][i])
             continue;
         setVisited(vvi, depth, i, n, 1);
         dfs(vvi, depth+1, n);
         setVisited(vvi, depth, i, n, -1);
     }
 }
示例#2
0
/*public*/
void
DirectedEdge::setVisitedEdge(bool newIsVisited)
{
	setVisited(newIsVisited);
	assert(sym);
	sym->setVisited(newIsVisited);
}
void
nsSMILInstanceTime::HandleChangedInterval(
    const nsSMILTimeContainer* aSrcContainer,
    bool aBeginObjectChanged,
    bool aEndObjectChanged)
{
  // It's possible a sequence of notifications might cause our base interval to
  // be updated and then deleted. Furthermore, the delete might happen whilst
  // we're still in the queue to be notified of the change. In any case, if we
  // don't have a base interval, just ignore the change.
  if (!mBaseInterval)
    return;

  NS_ABORT_IF_FALSE(mCreator, "Base interval is set but creator is not.");

  if (mVisited) {
    // Break the cycle here
    Unlink();
    return;
  }

  bool objectChanged = mCreator->DependsOnBegin() ? aBeginObjectChanged :
                                                      aEndObjectChanged;

  mozilla::AutoRestore<bool> setVisited(mVisited);
  mVisited = true;

  nsRefPtr<nsSMILInstanceTime> deathGrip(this);
  mCreator->HandleChangedInstanceTime(*GetBaseTime(), aSrcContainer, *this,
                                      objectChanged);
}
void
nsSMILInstanceTime::HandleChangedInterval(
    const nsSMILTimeContainer* aSrcContainer,
    PRBool aBeginObjectChanged,
    PRBool aEndObjectChanged)
{
  NS_ABORT_IF_FALSE(mBaseInterval,
      "Got call to HandleChangedInterval on an independent instance time.");
  NS_ABORT_IF_FALSE(mCreator, "Base interval is set but creator is not.");

  if (mVisited) {
    // Break the cycle here
    Unlink();
    return;
  }

  PRBool objectChanged = mCreator->DependsOnBegin() ? aBeginObjectChanged :
                                                      aEndObjectChanged;

  AutoBoolSetter setVisited(mVisited);

  nsRefPtr<nsSMILInstanceTime> deathGrip(this);
  mCreator->HandleChangedInstanceTime(*GetBaseTime(), aSrcContainer, *this,
                                      objectChanged);
}
示例#5
0
文件: Cell.cpp 项目: juvs91/wumpus
void Cell::init(const float pitProbability,int xIn,int yIn) {
    setVisited(false); //si lo pones en false el mapa se pone negro y se esconde todo
    //setVisited(true);
    setBreeze(false);
    setStench(false);
    setGlitter(false);
    setPosition(xIn, yIn);
    setPit(pitProbability);

}
示例#6
0
ProgramStrings Print::flatten() const
{
  if (isVisited()){
    return ProgramStrings();
  }
  
  setVisited(true);
 
  ProgramStrings prog;
  
  foreach(Sink *sink, sinks()){
    Data* sinkData = sink->sourceData();
    //ps += sinkData->flatten();
    prog = prog + sinkData->flatten();
  }
PRBool
nsSMILInstanceTime::IsDependentOn(const nsSMILInstanceTime& aOther) const
{
  if (mVisited)
    return PR_FALSE;

  const nsSMILInstanceTime* myBaseTime = GetBaseTime();
  if (!myBaseTime)
    return PR_FALSE;

  if (myBaseTime == &aOther)
    return PR_TRUE;

  // mVisited is mutable
  AutoBoolSetter setVisited(const_cast<nsSMILInstanceTime*>(this)->mVisited);
  return myBaseTime->IsDependentOn(aOther);
}
示例#8
0
bool
nsSMILInstanceTime::IsDependentOn(const nsSMILInstanceTime& aOther) const
{
  if (mVisited)
    return false;

  const nsSMILInstanceTime* myBaseTime = GetBaseTime();
  if (!myBaseTime)
    return false;

  if (myBaseTime == &aOther)
    return true;

  mozilla::AutoRestore<bool> setVisited(mVisited);
  mVisited = true;
  return myBaseTime->IsDependentOn(aOther);
}
bool
nsSMILInstanceTime::IsDependentOn(const nsSMILInstanceTime& aOther) const
{
  if (mVisited)
    return false;

  const nsSMILInstanceTime* myBaseTime = GetBaseTime();
  if (!myBaseTime)
    return false;

  if (myBaseTime == &aOther)
    return true;

  // mVisited is mutable
  mozilla::AutoRestore<bool> setVisited(const_cast<nsSMILInstanceTime*>(this)->mVisited);
  const_cast<nsSMILInstanceTime*>(this)->mVisited = true;
  return myBaseTime->IsDependentOn(aOther);
}
示例#10
0
文件: matching.c 项目: BelfordZ/cAlgs
/* u and v are the number of vertices in sets U, and V, respectively,
 * filling up bipgraph[0..u-1][0..v-1].
 * result:
 *  matching[u0]==v0 iff u0 and v0 are in the matching, 
 * otherwise matching[u0] = -1 */
void
match(int u, int v) {
  int i,j, head,tail, bad, last, increased;

  for( i = 0; i < u; i++ ) {
    matching[i] = -1;
    flagUmatched[i] = 0;
  }
  for( i = 0; i < v; i++ ) flagVmatched[i] = 0;

  do { /* find alternating paths by repeated bfs. */
    for( i = 0; i < MAXU+MAXV; i++ ) predecessor[i] = -1;
    for( i = 0; i < MAXU; i++ ) flagUused[i] = flagUvisited[i] = 0;
    for( i = 0; i < MAXV; i++ ) flagVused[i] = flagVvisited[i] = 0;
  
    head = tail = 0;

    /* put all the unmatched u's on the queue. They start the 
     * alternating path. */
    for( i = 0; i < u; i++ ) {
      if( ! isMatched(U(i))) {
	queue[tail++] = U(i);
	predecessor[i] = -1; /* redundant statement */
	setVisited(U(i));
      }
    }

    /* flag that at least one path was found by the bfs.
     * when the bfs does not find an alternating path we are done. */
    increased = 0;

    while( head != tail ) {
      i = queue[head++];

      /* this node appeared on some previously found alternating path. */
      if( isUsed(i) ) continue;
    
      if( isV(i) && !isMatched(i) ) {
	/* we got to the end of an alternating path. see if
	 * it is disjoint with other paths found so far. only
	 * then can we mess it up a bit. */
	bad = 0;
	for( j = i; j != -1; j = predecessor[j]) {
	  if( isUsed(j)) {
	    bad = 1;
	    break;
	  }
	}
	
	if( ! bad ) {
	  /* this path is pristine. switch "polarity" of edges
	   * in the matching on this path. */

	  /* flag and instrumention - whether (not) to quit,
	   * and how many paths we found this bfs. */
	  increased++; 
	  for( j = i; j != -1; last = j, j = predecessor[j] ) {
	    if( isV(j) && !isMatched(j)) {
	      /* the only unmatched v - actually this means we
	       * are on the first iteration of this loop. */
	      setMatched(j);

	    } else if( isU(j) ) {
	      if( isMatched(j) ) {
		/* the node we saw in the previous iteration of
		 * this loop must be a V. We will match with it
		 * instead of the one we used to match with, which
		 * must be the next node visited in this loop. */
		assert(isV(last));
		matching[j] = last - MAXU;
	      } else {
		/* we are the very first u, one of the ones the
		 * bfs queue was "seeded" with. We should have ...*/
		assert(predecessor[j] == -1);
		setMatched(j);
		assert(isV(last));
		matching[j] = last - MAXU;
	      }
	    }
	    setUsed(j); /* this node cannot be used for other
			 * paths we might run across in the future
			 * on this bfs. */
	  } /* for */
	} /* if ! bad */
      } /* isV and !isMatched */

      else if( isV(i) ) {
	/* this must be a matched V - find the matching U and put it on 
	 * the queue if it is not visited or used. */

	bad = 1;

	for( j = 0; j < u; j++ ) {
	  if( isMatched(U(j)) && matching[j] == i - MAXU ) {
	    /* this is the one. */
	    if( ! isVisited(U(j)) && !isUsed(U(j))) {
	      setVisited(U(j));
	      queue[tail++] = U(j); 
	      predecessor[U(j)] = i;
	    }
	    bad = 0;
	    break;
	  }
	}
	assert(!bad);
      } /* isV */
      else if( isU(i) ) {
	/* we are at U. whether it is unmatched (a "seed"),
	 * or matched, we do the same thing - put on the queue
	 * all V's which it is connected to in the graph but
	 * which it is _not_ paired to in the current matching. */

	for( j = 0; j < v; j++ ) {
	  if( bipgraph[i][j] &&
	      !isVisited(V(j)) && 
	      !isUsed(V(j)) && 
	      matching[i] != j ) {
	    /* we can put this one on the queue. */
	    queue[tail++] = V(j);
	    predecessor[V(j)] = i;
	    setVisited(V(j));
	  }
	}
      } else {
	assert(0); /* should be no other cases. */
      }
      /* this is the end of the bfs. */
    } 
  } while( increased );

  return;
}
示例#11
0
int main(int argc, char** argv){

	//Reads in file containing weighted graph
	char string[15];
	printf("Enter file name: ");
	scanf("%s", &string);
	
	//Scans file and error checks existence of input file
	FILE* input = fopen(string, "r");
	if(input == NULL){
		printf("File not found\n");
		return 1;
	}
	
	//Scans first line to determine type of graph
	char type[10];
	fscanf(input, "%s", type);
	
	//Scans second line of file to find the number of vertices
	int vertices;
	fscanf(input, "%d", &vertices);
		
	//creates array of linked lists
	List* list[vertices];
	int i;
	for(i=0; i<vertices; i++){
		list[i] = NULL;
	}
	
	//scans file and places information into linked lists
	char buffer[MAX_LINE_SIZE];
	char* token;
	while(fgets(buffer, MAX_LINE_SIZE, input))
	{
		//Error checking
		if(buffer[strlen(buffer)-1] == '\n')
			buffer[strlen(buffer)-1] = '\0';
			
		//Skips tabs, new lines, and spaces
		token = strtok(buffer, " \n\r\t");
			
		//Places destinations into correct vertices
		int vertex, destination, weight;
		while(token != NULL)
		{
			sscanf(token, "(%d,%d,%d)", &vertex, &destination, &weight);
			list[vertex-1] = createList(list[vertex-1], destination-1, weight);
			
			if(strncmp(type, "UNDIRECTED", 10) == 0){
				list[destination - 1] = createList(list[destination-1], vertex-1, weight);
			}
			
			token = strtok(NULL, " \n\r\t");
		}
	}

	// Prints adjacency list
	printList(list, vertices);
	
	if(strncmp(type, "DIRECTED", 10) == 0){
		
		printf("\nDistances:\n");
		dijkstra(vertices, list);
	}
	else if(strncmp(type, "UNDIRECTED", 10) == 0){
		
		SCC* scc = NULL;
		int graphCount =0;
		int* visited = malloc(sizeof(int)*vertices);
		visited = setVisited(visited, vertices);
		for(i=0; i<vertices; i++){
			if(visited[i] == 2){
				visited = DFS(list, i, vertices);
				scc = createVisited(scc, visited);
				graphCount++;
			}
		}
		
		if(graphCount > 1)
			printf("The graph as a whole is not strongly connected\n");
		else
			printf("The graph is strongly connected\n");
			
		//Prints the strongly connected components of the graph
		printSCC(scc, vertices);
		
		printf("\n");
		
		if(graphCount == 1)
			prim(vertices, list);
			
		free(visited);
	}
	
	close(input);
	
	return 0;
}
示例#12
0
BMP MazeGen::Generate()
{
	// Create a vertex with coordinates within (1, 1) and (Width-1, Height-1), 
	// and set it's visited member to true.
	Vertex current;
	current.x = (rand() % (Width / 2)) * 2 + 1;
	current.y = (rand() % (Height/ 2)) * 2 + 1;
        setVisited(current);

	// Push the starting cell to the stack
	stk.push(current);

	while(!AllCellsVisited())
	{
		// Get all neighbors of the current cell
		std::vector<Vertex> neighbors = GetNeighbors(current);
		
		// Get a random unvisited neighbor of the current cell, or discover that there are no unvisited neighbors
		int randomNeighbor = rand() % neighbors.size();
		
		// While neighbors[randomNeighbor] is a cell that has been visited already:
		while( getVisited(mazeArray[neighbors[randomNeighbor].x][neighbors[randomNeighbor].y]) == true)
		{
			// Remove that random neighbor from the vector of neighbors
			neighbors.erase(neighbors.begin() + randomNeighbor);

			// If there are no neighbors left, break the loop
			if(neighbors.empty()) break;

			// Pick a new random neighbor
			randomNeighbor = rand() % neighbors.size();
		}

		// If there are no unvisited neighbors of the current cell
		if(neighbors.empty())
		{
			// Go back a step, popping the last cell off the stack and setting it to the current cell, 
			// then repeating the whole process
			current = stk.top();
			stk.pop();
		}

		// If there was an unvisited neighbor of of the current cell
		else
		{
			// Remove the 'wall' between the current cell and it's neighbor, drawing a line between the two cells
			RemoveWall(current, neighbors[randomNeighbor]);
			
			// Set the current vertex to the randomly selected neighbor
			current = neighbors[randomNeighbor];

			// Mark it as visted
			setVisited(mazeArray[current.x][current.y]);

			// Push it to the stack
			stk.push(current);
		}
	}

	return maze;
}