Example #1
0
void SearchGraph::bfs() {
  Node* n;

  explored.clear();
  bfsInsert(root);

  while(true) {
    n = bfsRemove();
    if(!n) {
      cout << "No Solution" << endl;
      return;
    }

    if(nodeIsGoal(n)) {
      return solution(n);
      //return printDot();
    }

    if(!alreadyVisited(n->r, n->c)) {
      n->expand();
      bfsInsertList(n->children);
    } else {
      n->visited = true;
    }
  }
}
void depthFirstSearch(Node node, char* visited, int* count) {
	printf("%c", node.name);
	visited[*count] = node.name; *count = *count + 1;
	if (node.neighborCount != 0) {
		for (int i = 0; i < node.neighborCount; i++) {
			if(!alreadyVisited(visited, node.neighbors[i]->name, *count))
				depthFirstSearch(*node.neighbors[i], visited, count);
		}
	}
}
Example #3
0
/*
* Descend through the hierarchy, starting at "fullpath".
* If "fullpath" is anything other than a directory, we lstat() it,
* call the search function, and return. For a directory, we call ourself
* recursively for each name in the directory.
*/
static int /* we return whatever func() returns */

dopath(char *searchstr, struct FlagOpts *flags)
{
	struct stat statbuf;
	struct dirent *dirp;
	DIR *dp;
	int ret;
	char *ptr;
	
	int errnum;
	errno = 0;

	if (lstat(fullpath, &statbuf) < 0) { /* stat error */
		errnum = errno;
		my_errprintf("Error reading file attribute: %s\n", strerror(errnum) );
	}

	if (S_ISLNK(statbuf.st_mode) != 0) { /* symbolic link */
		if( (*flags).l == 1) { // search symbolic links
			int k = readlink( fullpath, symlpath, PATH_MAX+1 );
			symlpath[k] = '\0';
			if (stat(fullpath, &statbuf) < 0) { /* stat error */
				errnum = errno;
				my_errprintf("Error reading file attribute: %s\n", strerror(errnum) );
			}
		} 
		else { // do not search file or folder
			return 0;
		}
	}

	// Do not read already visited files
	if ( alreadyVisited(statbuf.st_ino) == 1 ) {
		return 0;
	}
	
	if (S_ISDIR(statbuf.st_mode) == 0) { /* not a directory */
		// Skip certain files according to [-f] flags used
		if ( fileTypesFlag == 1 )
		{
			if ( checkEnding('c',fullpath)==1 ) {
				if( flags->c == 0) return 0;
			}
			else if ( checkEnding('h',fullpath)==1 ) {
				if( flags->h == 0) return 0;
			}
			else if ( checkEnding('S',fullpath)==1 ) {
				if( flags->S == 0) return 0;
			}
			else { //other type of file - skip since flag opt used
				return 0;
			}
		}

		// add regular file i-node to list of files visited
		markVisited(statbuf.st_ino);
		return(readfile(fullpath, searchstr, &statbuf, FTW_F));
	}

	/*
* It's a directory. First call func() for the directory,
* then process each filename in the directory.
*/
	// add directory i-node to list of files visited
	markVisited(statbuf.st_ino);

	if ((ret = readfile(fullpath, searchstr, &statbuf, FTW_D)) != 0)
		return(ret);

	ptr = fullpath + strlen(fullpath); /* point to end of fullpath */
	*ptr++ = '/';
	*ptr = 0;

	if ((dp = opendir(fullpath)) == NULL) /* can't read directory */
		return(readfile(fullpath, searchstr, &statbuf, FTW_DNR));

	while ((dirp = readdir(dp)) != NULL) {
		if (strcmp(dirp->d_name, ".") == 0 ||
				strcmp(dirp->d_name, "..") == 0)
			continue; /* ignore dot and dot-dot */
		strcpy(ptr, dirp->d_name); /* append name after slash */
		if ((ret = dopath(searchstr, flags)) != 0) /* recursive */
			break; /* time to leave */
	}

	ptr[-1] = 0; /* erase everything from slash onwards */
	if (closedir(dp) < 0) ;
	return(ret);
}
Example #4
0
void Pathfinder::expandNode(PathNode* node)
{
	Animation::Sequence walkanim = Animation::walk;
	PathfindingState state, closeststate;
	AnimationTracker tracker;
	expandednodes++;

	if (actor->isInCombat())
		walkanim = Animation::advance;

	// try walking in all 8 directions
	for (uint32 dir = 0; dir < 8; ++dir) {
		state = node->state;
		state.lastanim = walkanim;
		state.direction = dir;
		state.combat = actor->isInCombat();
		uint32 steps = 0, beststeps = 0;
		int bestsqdist;
		bestsqdist = (targetx - node->state.x + actor_xd/2) *
			(targetx - node->state.x + actor_xd/2);
		bestsqdist += (targety - node->state.y + actor_yd/2) *
			(targety - node->state.y + actor_yd/2);

		if (!tracker.init(actor, walkanim, dir, &state)) continue;

		// determine how far the actor will travel if the animation runs to completion
		sint32 max_endx, max_endy;
		tracker.evaluateMaxAnimTravel(max_endx, max_endy, dir);
		if(alreadyVisited(max_endx, max_endy, state.z)) continue;
		int sqrddist;
		int x_travel = abs(max_endx - state.x);
		int xy_maxtravel = x_travel;	// don't have the max(a,b) macro...
		int y_travel = abs(max_endy - state.y);
		if(y_travel > xy_maxtravel) xy_maxtravel = y_travel;

		sqrddist = x_travel * x_travel + y_travel * y_travel;
		if(sqrddist > 400)
		{
			// range is greater than 20; see if a node has been visited at range 10
			if(alreadyVisited(	state.x + x_travel * 10 / xy_maxtravel,
								state.y + y_travel * 10 / xy_maxtravel,
								state.z)) {
				continue;
			}
		}
		while (tracker.step())
		{
			steps++;
			tracker.updateState(state);

			int sqrddist;
			sqrddist = (targetx - state.x + actor_xd/2) *
				(targetx - state.x + actor_xd/2);
			sqrddist += (targety - state.y + actor_yd/2) *
				(targety - state.y + actor_yd/2);

			if (sqrddist < bestsqdist) {
				bestsqdist = sqrddist;
				beststeps = steps;
				closeststate = state;
			}
		}

		if (tracker.isDone()) {
			tracker.updateState(state);
			if (!alreadyVisited(state.x, state.y, state.z)) {
				newNode(node, state, 0);
				visited.push_back(state);
			}
		}
		else
		{
			// an obstruction was encountered, so generate a visited node to block
			// future evaluation at the endpoint.
			visited.push_back(state);
		}

		// TODO: maybe only allow partial steps close to target?
		if (beststeps != 0 && (beststeps != steps ||
							   (!tracker.isDone() && targetitem)))
		{
			newNode(node, closeststate, beststeps);
			visited.push_back(closeststate);
		}
	}
}