void Npc::doMoveTo(const Position& target) { std::forward_list<Direction> listDir; if (getPathTo(target, listDir, 1, 1, true, true)) { startAutoWalk(listDir); } }
int main(){ Graph G; G = NULL; G = newGraph (6); if(G){ printf("Graph Created\n"); }else{ printf("Graph Not Created\n"); } addEdge(G,0,1); addEdge(G,1,2); addEdge(G,1,3); addEdge(G,3,4); addEdge(G,4,2); doBFS(G,0); printf("%d\n", getDistance(G,0)); printf("%d\n", getDistance(G,1)); printf("%d\n", getDistance(G,2)); printf("%d\n", getDistance(G,3)); printf("%d\n", getDistance(G,4)); printf("%d\n", getDistance(G,5)); printList(stdout,getPathTo(G,4)); /* freeGraph(&G); */ return(0); }
void Creature::goToFollowCreature() { if (followCreature) { FindPathParams fpp; getPathSearchParams(followCreature, fpp); Monster* monster = getMonster(); if (monster && !monster->getMaster() && (monster->isFleeing() || fpp.maxTargetDist > 1)) { Direction dir = DIRECTION_NONE; if (monster->isFleeing()) { monster->getDistanceStep(followCreature->getPosition(), dir, true); } else { //maxTargetDist > 1 if (!monster->getDistanceStep(followCreature->getPosition(), dir)) { // if we can't get anything then let the A* calculate listWalkDir.clear(); if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { hasFollowPath = true; startAutoWalk(listWalkDir); } else { hasFollowPath = false; } return; } } if (dir != DIRECTION_NONE) { listWalkDir.clear(); listWalkDir.push_front(dir); hasFollowPath = true; startAutoWalk(listWalkDir); } } else { listWalkDir.clear(); if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { hasFollowPath = true; startAutoWalk(listWalkDir); } else { hasFollowPath = false; } } } onFollowCreatureComplete(followCreature); }
bool Creature::getPathTo(const Position& targetPos, std::forward_list<Direction>& dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 0*/) const { FindPathParams fpp; fpp.fullPathSearch = fullPathSearch; fpp.maxSearchDist = maxSearchDist; fpp.clearSight = clearSight; fpp.minTargetDist = minTargetDist; fpp.maxTargetDist = maxTargetDist; return getPathTo(targetPos, dirList, fpp); }
int main(int argc, char** argv) { int numVerts; int numQueries; int from; char edge[1024]; int to; int start; int end; char *filename = argv[1]; FILE *input = fopen(filename, "r"); int i = 0; char *c; if(input != NULL) { fscanf(input, "%d", &numVerts); fscanf(input, "%d", &numQueries); /*printf("%d %d\n", numVerts, numQueries);*/ Graph G = newGraph(numVerts); for(i = 0; i < numVerts; i++) { fscanf(input, "%d", &from); /*printf("%d ", from);*/ fgets(edge, 1024, input); c = strtok(edge, " \n"); while(c != NULL) { addEdge(G, from, atoi(c)); c = strtok(NULL, " \n"); } } for(i = 0; i < numQueries; i++) { fscanf(input, "%d", &start); fscanf(input, "%d", &end); doBFS(G, start); if(getDistance(G, end) > 0) { printf("The shortest path from %d to %d requires %d edges:\n", start, end, getDistance(G, end)); ListHndl L = getPathTo(G, end); } else { printf("No path from %d to %d exists.\n", start, end); } } } fclose(input); return(0); }
/* main program*/ int main(int argc, char *argv[]){ for(int i = 1; i < argc; i++){ // open file and read in numVerts and numQueries FILE* in = fopen(argv[i], "r"); int numVerts, numQueries; char tempLine[1024]; fgets(tempLine, 1024, in); sscanf(tempLine, "%d %d", &numVerts, &numQueries); // read in and make graph Graph Map = newGraph(numVerts); for(int i = 0; i < numVerts; i++){ fgets(tempLine, 1024, in); int vert = atoi(strtok(tempLine, " ")); char *tempTo = strtok(NULL, " "); while(tempTo != NULL){ addEdge(Map, vert, atoi(tempTo)); tempTo = strtok(NULL, " "); } } // process queries for(int i = 0; i < numQueries; i++){ int from, to; fscanf(in, "%d %d", &from, &to); doBFS(Map, from); if(getDistance(Map, to) == -1) printf("No path from %d to %d exists.\n\n", from, to); else{ printf("The shortest path from %d to %d requires %d edges: \n", from, to, getDistance(Map, to)); ListHndl L = getPathTo(Map, to); moveFirst(L); while(!atLast(L)){ printf("%d -> ", getCurrent(L)); moveNext(L); } printf("%d \n\n", getCurrent(L)); freeList(&L); } } /*free everythingggggggggggg*/ freeGraph(&Map); fclose(in); } /* end program */ return 0; }
int main(int argc, char** argv) { Graph G = newGraph(5); addEdge(G, 0, 1); addEdge(G, 0, 2); addEdge(G, 2, 3); addEdge(G, 2, 4); addEdge(G, 3, 4); addEdge(G, 4, 3); addEdge(G, 4, 2); doBFS(G, 0); printf("To get from 0 to 4 requires %d edges.\n",getDistance(G, 0)); ListHndl L = getPathTo(G, 4); printf("Follow the path "); printList(L); return(0); }
std::vector<State*> SearchTree::doSearch(std::string algorithm) { double startT, finishT; #ifdef __unix timespec startWallTime, finishWallTime; clock_gettime(CLOCK_MONOTONIC, &startWallTime); #else GET_TIME(startT); #endif std::clock_t startCpuTime = std::clock(); if (algorithm == "bcktrk") solution = backTracking(root); else if (algorithm == "dfs") solution = depthFirstSearch(dfsDepthLimit); else if (algorithm == "bfs") solution = breadthFirstSearch(); else if (algorithm == "ucs") solution = orderSearch(); else if (algorithm == "greedy") solution = greedy(); else if (algorithm == "astr") solution = AStar(); else if (algorithm == "idastr") solution = IDAStar(); searchCpuTime = (std::clock() - startCpuTime) / (double)CLOCKS_PER_SEC; #ifdef __unix clock_gettime(CLOCK_MONOTONIC, &finishWallTime); searchWallTime = (finishWallTime.tv_sec - startWallTime.tv_sec) + (finishWallTime.tv_nsec - startWallTime.tv_nsec) / 1000000000.0; #else GET_TIME(finishT); searchWallTime = finishT - startT; #endif return getPathTo(solution); }
int main (int argc, char *argv[]) { int numVerts; int numQueries; int numDigits; char buffer[1024]; char *pbuff; int i; int num; int tempNum; int digits; FILE *file; char fileName[1000]; argc = argc; file = fopen (argv[1], "r"); /*read in numbers from first file input*/ pbuff = malloc(sizeof(char) * 1024); fgets(buffer, sizeof buffer, file); pbuff = buffer; sscanf(pbuff, "%d %d", &numVerts, &numQueries); GraphHndl TheGraph; TheGraph = NULL; TheGraph = newGraph(numVerts); /*reading in the edges*/ for(i = 0; i < numVerts && fgets(buffer, sizeof buffer, file); i++) { pbuff = buffer; /*read in first vertex */ sscanf(pbuff, "%d", &num); /*loop to count the digits to format and read properly*/ digits = 0; tempNum = num; while(tempNum != 0) { tempNum /= 10; digits++ ; } pbuff += (digits + 1); while(sscanf(pbuff, "%d", &num) == 1) { digits = 0; tempNum = num; while(tempNum != 0) { tempNum /= 10; digits++ ; } /*uncomment next line to print all the edge inserts*/ /*printf("Inserting %d into %d's adjacency list\n", num, i);*/ addEdge(TheGraph, i, num); pbuff += (digits + 1); } } /*uncomment to print the graph*/ /* printGraph(TheGraph);*/ /* time for queries, I will reuse num for the from integer and tempNum for the to integer*/ for(i = 0; i < numQueries && fgets(buffer, sizeof buffer, file); i++) { pbuff = buffer; sscanf(pbuff, "%d %d", &num, &tempNum); /*doBFS(TheGraph, num);*/ if(isEmpty(getPathTo(TheGraph, tempNum)) == 1) { printf("There is no way to get from %d to %d in this graph.\n", num, tempNum); } else { printf("To get from %d to %d requires %d edges, follow the path %d ", num, tempNum, getDistance(TheGraph, tempNum), num); printList(getPathTo(TheGraph, tempNum)); } } freeGraph(TheGraph); return 0; }
int main(int argc, char **argv) { FILE *inFile; char str[20000]; char *token; int nVerts; int nReqs; Graph pathGraph; /* * Validate input file * */ if (argc < 2) { perror("Please specify an input file\n"); return (-1); } inFile = fopen(argv[1], "r"); if (inFile == NULL) { perror("Could not open input file"); return (-1); } /* * Read Input file * */ fgets(str, 60, inFile); strtok(str, "\n"); token = strtok(str, " "); nVerts = atoi( token ); token = strtok(NULL, " "); nReqs = atoi( token ); /* * Build Graph & Insert Edges * */ pathGraph = newGraph( nVerts ); for (int from = 0; from < nVerts; ++from ) { fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); token = strtok (NULL, " "); while( token != NULL) { int to = atoi( token ); insertEdge( pathGraph, from, to); token = strtok (NULL, " "); } } /* * Process requests and find paths * */ for (int from = 0; from < nReqs; ++from ) { int source; int dest; fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); source = atoi( token ); token = strtok(NULL, " "); dest = atoi( token ); doBFS(pathGraph, source); int dist = getDistance( pathGraph, dest); if ( dist > -1) { printf("The shortest path from %d to %d requires %d edge(s):\n", source, dest, dist); ListHndl path = getPathTo( pathGraph, dest); moveFirst( path ); while( !offEnd( path )) { printf("%d ", getCurrent( path)); if ( !atLast( path )) { printf( "-> "); } else { printf("\n\n"); } moveNext( path ); } freeList( path ); } else { printf("No path from %d to %d exists\n\n", source, dest); } } fclose (inFile); freeGraph(pathGraph); return 0; }