/** * Creates odd paths */ void createOddPaths() { // Find odd paths oddPaths = malloc(sizeof (int) *MAX_PATH_LENGTH * oddNodesSize / 2); oddPathsSizes = malloc(sizeof (int) *oddNodesSize / 2); oddPathsSize = 0; int i; for (i = 0; i < oddNodesSize; i += 2) { Path_element *path = findShortestPath( getNodeById(oddNodes[i])->positionGrid.x, getNodeById(oddNodes[i])->positionGrid.y, 1, getNodeById(oddNodes[i + 1])->positionGrid.x, getNodeById(oddNodes[i + 1])->positionGrid.y ); oddPaths[oddPathsSize] = malloc(sizeof (int) *MAX_PATH_LENGTH); oddPathsSizes[oddPathsSize] = 0; while (path) { oddPaths[oddPathsSize][oddPathsSizes[oddPathsSize]++] = path->id; path = path->next; } // printf("Path found, size: %d\n", oddPathsSizes[oddPathsSize]); oddPathsSize++; } }
/** * Get Node rating */ int getNodeRatingClean(int i) { int cons = 0; if (getNodeById(i)->north) cons++; if (getNodeById(i)->south) cons++; if (getNodeById(i)->east) cons++; if (getNodeById(i)->west) cons++; return cons; }
/* Swaps two nodes in place based on node ids */ Bool swap(char * argA , char * argB) { int idA = atoi(argA), idB = atoi(argB); Node *nodeA = getNodeById(idA), *nodeB = getNodeById(idB); if (!nodeA || !nodeB) { return False; } else { swapNodes(nodeA, nodeB); return True; } }
/** * Finds odd nodes */ void getOddNodes() { int i; oddNodesSize = 0; oddNodes = malloc(sizeof (int) *NUM_NODES); for (i = 0; i < NUM_NODES; i++) { // Now count nodes Node* node = getNodeById(i); int nbs = 0; if (node->south) nbs++; if (node->north) nbs++; if (node->east) nbs++; if (node->west) nbs++; if (nbs % 2 == 1) { // This is an odd node oddNodes[oddNodesSize++] = i; node->odd = 1; } else if (nbs == 0) { // printf("Isolated node found [%d]\n", i); } } // printf("%d odd node(s) found.\n", oddNodesSize); }
void deleteRecord(struct DB *db, char *buf) { unsigned long sid = strtoul(buf,NULL,10); struct Node *theNode; printf("Delete %lu\n", sid); theNode = getNodeById(db->lname_db, sid); deleteNode(theNode); theNode = getNodeById(db->fname_db, sid); deleteNode(theNode); theNode = getNodeById(db->sid_db, sid); deleteNode(theNode); theNode = getNodeById(db->gpa_db, sid); deleteNode(theNode); }
/** * Gets node ratings * @param nd * @return */ int getNodeRating(int nd) { // Get number of connections it can possibly have int cons = 0, i, j, k; i = nd; if (getNodeById(i)->north) cons++; if (getNodeById(i)->south) cons++; if (getNodeById(i)->east) cons++; if (getNodeById(i)->west) cons++; // Now substract all the connections // Odd paths first for (j = 0; j < oddPathsSize; j++) { if (oddPaths[j][0] == i) cons--; if (oddPaths[j][oddPathsSizes[j] - 1] == i) cons--; for (k = 1; k < oddPathsSizes[j] - 1; k++) { if (oddPaths[j][k] == i) cons -= 2; } } // Now even paths for (j = 0; j < evenPathsSize; j++) { if (evenPaths[j]) { // Path must be initialized, function could be called when this is not the case so just check to be sure if (evenPaths[j][0] == i) { cons--; } if (evenPaths[j][evenPathsSizes[j] - 1] == i) { cons--; } for (k = 1; k < evenPathsSizes[j] - 1; k++) { if (evenPaths[j][k] == i) { cons -= 2; } } } } return cons; }
/** * Determines the total path length between two nodes * @param node1 * @param node2 * @return */ int getLengthBetweenNodes(int node1, int node2) { Node* from = getNodeById(node1); Node* to = getNodeById(node2); Path_element *path = findShortestPath( from->positionGrid.x, from->positionGrid.y, 1, to->positionGrid.x, to->positionGrid.y ); int length = 0; while (path) { length += path->step_weight; path = path->next; } //free(path); return length; }
void focus(char * argA, char * argB) { if (!focusedNode) return; int delta = atoi(argB); Node * newFocus = NULL; if (!strcmp(argA, "id")) { newFocus = getNodeById(delta); } else if (!strcmp(argA, "brother")) { newFocus = getBrother(focusedNode, delta); } else if (!strcmp(argA, "pc")) { while (delta != 0) { newFocus = (delta < 0) ? focusedNode -> parent : focusOrChildOf(focusedNode); delta = delta + ( delta > 0 ? -1 : 1); focusNode(newFocus, NULL, True, True); } return; } focusNode(newFocus, NULL, True, True); }
result_t HeapSnapshot::get_root(obj_ptr<HeapGraphNode_base>& retVal) { return getNodeById(1, retVal); }
/** * Creates even paths */ void createEvenPaths() { evenPaths = malloc(sizeof (int) *MAX_PATH_LENGTH * MAX_PATHS); evenPathsSizes = malloc(sizeof (int) *MAX_PATHS); evenPathsSize = 0; while (getFreeNodes()[0] != 1) { evenPathsSize++; evenPathsSizes[evenPathsSize - 1] = 0; evenPaths[evenPathsSize - 1] = malloc(sizeof (int) *MAX_PATH_LENGTH); // printf("Finding single even node\n"); int *freeNodes = getFreeNodes(); int i, now = -1;//freeNodes[0]; for (i = 1; i < freeNodes[0]; i++) { if (getNodeRating(freeNodes[i]) == 2) { now = freeNodes[i]; } } // now = freeNodes[1]; if (now == -1) { printf("An error occurred!\n"); break; } printf("Backtracing even path [starting at node %d]\n", now); int start = now, startup = 1, prev = -1; // While now is in free nodes (it wont be as soon as the path is closed) while (start != now || startup) { startup = 0; // Add now to current Path evenPaths[evenPathsSize - 1][evenPathsSizes[evenPathsSize - 1]++] = now; // Find a neighbouw who also is in freeNodes if (getNodeById(now)->south && validEvenPathPartner(now, getNodeById(now)->south->id, prev)) { prev = now; now = getNodeById(now)->south->id; continue; } if (getNodeById(now)->east && validEvenPathPartner(now, getNodeById(now)->east->id, prev)) { prev = now; now = getNodeById(now)->east->id; continue; } if (getNodeById(now)->west && validEvenPathPartner(now, getNodeById(now)->west->id, prev)) { prev = now; now = getNodeById(now)->west->id; continue; } if (getNodeById(now)->north && validEvenPathPartner(now, getNodeById(now)->north->id, prev)) { prev = now; now = getNodeById(now)->north->id; continue; } printf("Erroneous even path!\n"); // getchar(); break; } // YEAH PATH COMPLETE, NOW ADD START TO END evenPaths[evenPathsSize - 1][evenPathsSizes[evenPathsSize - 1]++] = now; // printf("Backtracing complete, length %d\n", evenPathsSizes[evenPathsSize - 1]); } // Now pray it works T_T }
/** * Second main function * @return */ int main_2() { // Initializations Position start, destination; Path_element *path = NULL; Path_element *robot_path; int x, y; #ifdef CHALLENGE_AB int initial_facing_direction; int i; static int first_run = 1; if (first_run) { first_run = 0; // Get which places to user wants to visit getPlacesToVisit(); } // Reset no_total_path_possible no_total_path_possible = 0; #endif // Unvisit all places unvisitAll(); // Seed the rand() function srand(time(NULL)); #ifdef CHALLENGE_AB // Randomize facing direction initial_facing_direction = NORTH;// = rand() % 4 + 1; #endif // Initialize grid generateGrid(); #if MINES_ACTIVE == 1 // Create mines // createMines(); // removeConnection(0,0,1,0); // removeConnection(1,0,2,0); // removeConnection(0,2,1,2); // removeConnection(2,2,3,2); // removeConnection(3,3,4,3); // removeConnection(0,0,0,1); // removeConnection(1,0,1,1); // removeConnection(1,3,1,4); // removeConnection(2,1,2,2); // removeConnection(2,3,2,4); // removeConnection(3,0,3,1); // removeConnection(4,0,4,1); // removeConnection(4,1,4,2); // removeConnection(0,0,0,1); // removeConnection(1,3,1,2); // removeConnection(2,2,2,3); // removeConnection(4,1,4,2); #endif // Define the start position in the bottom left start.x = destination.x = 1; start.y = destination.y = 0; #if MINES_ACTIVE == 1 // Discover mines at start location // discoverMines(start.x, start.y); #endif #ifdef CHALLENGE_AB int key, q; goto go2; frombeginning2: cls_2(); printf("Place the robot at the beginning, type '1' to continue...\n"); key = 0; while (!key) { scanf("%d",&key); } go2: curDir = 0; setCommand(new_sck, MOVE, 1, 1, 0); wait_till_ready(new_sck); setCommand(new_sck, POS, 0, start.x+1, start.y+1); wait_till_ready(new_sck); // Get first destination destination.x = places_to_visit[getTargetPlace(start.x, start.y, initial_facing_direction, 0)][0]; destination.y = places_to_visit[getTargetPlace(start.x, start.y, initial_facing_direction, 0)][1]; // Create the robot_path linked list robot_path = malloc(sizeof (Path_element)); robot_path->x = start.x; robot_path->y = start.y; robot_path->facing_direction = initial_facing_direction; robot_path->next = NULL; for (q = 0; q < NUM_NODES; q++) getNodeById(q)->mark = 0; // Mark places to visit for (i = 0; i < NUMBER_OF_PLACES_TO_VISIT; i++) { if (!places_to_visit[i][2]) grid[places_to_visit[i][0]][places_to_visit[i][1]]->mark = 49 + i; } // Find initial path path = findShortestPath(start.x, start.y, initial_facing_direction, destination.x, destination.y); // If path == NULL, there was no path found if (!path || no_total_path_possible) { // Clear screen, print the grid, display a message, sleep for a while and then rerun the program cls_2(); printGrid(robot_path); printf("\nCouldn't find a intial path...\n"); // getchar(); // main(); return 0; } // Record start x = start.x; y = start.y; // Display first move cls_2(); printGrid(robot_path); // getchar(); int moveCorrect = 1; while (1) { if (kbhit()) goto frombeginning2; if (moveCorrect) path = path->next; // addToEndOfPath(&robot_path, path->x, path->y); if (x == path->x && y == path->y) { path = path->next; } int prevfd = path->facing_direction; if (makeMove(x,y,path->x,path->y)) { moveCorrect = 1; x = path->x; y = path->y; } else { printf("Returned mine between (%d,%d)<curr pos> and (%d,%d)\n",x,y,path->x,path->y); removeConnection(x,y,path->x,path->y); moveCorrect = 0; // prevfd = reverseDirection(prevfd); } // Move and save position and step weight, also add the position to the path if (moveCorrect) { path = path->next; addToEndOfPath(&robot_path, x, y); } // printf("VISIT\n"); visit(x, y); // printf("VISITED ALL PLACES\n"); if (visitedAllPlaces()) break; // printf("getTargetPlace\n"); // Get destination destination.x = places_to_visit[getTargetPlace(x, y, prevfd, 0)][0]; destination.y = places_to_visit[getTargetPlace(x, y, prevfd, 0)][1]; // #if MINES_ACTIVE == 1 // Check for mines // discoverMines(x, y); // Now filter combs for discovered mines // #endif // Calculate new path, mines couldve changed something // printf("New path %d,%d %d %d, %d\n",x,y,prevfd,destination.x,destination.y); path = findShortestPath( x, y, prevfd, destination.x, destination.y ); // printf("kay\n"); // If path == NULL, there was no path found if (!path || no_total_path_possible) { cls_2(); printGrid(robot_path); printf("Could not find a path!\n"); getchar(); return 0; } // Display path and add step weight to path weight cls_2(); printGrid(robot_path); getTargetPlace(x, y, path->facing_direction, DEBUG); // getchar(); } // #if MINES_ACTIVE == 1 // // Reveal mines // revealMines(); // #endif // Display final screen cls_2(); printGrid(robot_path); printf("\nDone.\n"); getchar(); #endif #ifdef CHALLENGE_C // Find initial path int *combs; int index; int q; // clear grid markings // Some unreadable shit which makes things work like they should cls_2(); printf("Press enter to start...\n"); getchar(); int antiMine = 0; rerun: goto go3; antimine: antiMine = 1; go3: stepsTakenSize = 0; index = 0; int key; goto go2; frombeginning: cls_2(); printf("Place the robot at the beginning, type '1' to continue, '9' to restart or '5' to restart in anti-mine mode...\n"); key = 0; while (key==0) { scanf("%d",&key); } if(key==9) goto rerun; if(key==5) goto antimine; go2: curDir = 0; setCommand(new_sck, MOVE, 1, 1, 0); wait_till_ready(new_sck); for (q = 0; q < NUM_NODES; q++) getNodeById(q)->mark = 0; robot_path = malloc(sizeof (Path_element)); robot_path->x = start.x; robot_path->y = start.y; robot_path->next = NULL; cls_2(); printGrid(robot_path); setCommand(new_sck, POS, 0, start.x+1, start.y+1); wait_till_ready(new_sck); x = start.x; y = start.y; // Find initial path combs = exploreArea(grid[start.x][start.y]->id); // If path == NULL, there was no path found if (!combs) { // Clear screen, print the grid, display a message, sleep for a while and then rerun the program printf("\nCouldn't find a intial path...\n"); getchar(); // main(); return 0; } while (1) { // Start by removing the path walked from the current combs int i;//, fins = 1; for (i = 0; combs[i] != -2; i+=2) { if (inStepsTaken(combs[i],combs[i+1])) { combs[i] = combs[i+1] = -1; } } // Check if there is a comb available if (combs[index] == -1) { index += 2; continue; } if (combs[index] == -2) break; // First walk to first node int nodeNow = combs[index]; int nodeTo = combs[index+1]; //printf path = findShortestPath(x, y, (path?path->facing_direction:NORTH), // 1, getNodeById(nodeNow)->positionGrid.x, getNodeById(nodeNow)->positionGrid.y); int failed = 0; int intcpt = 0; while (!(x == getNodeById(nodeNow)->positionGrid.x && y == getNodeById(nodeNow)->positionGrid.y)) { if (kbhit()) goto frombeginning; if (path && x == path->x && y == path->y) path = path->next; else if (!path) { printf("FAIL!\n"); getchar(); intcpt = 1; failed = 1; break; } // printf("Making first MOVE!\n"); if (makeMove(x,y,path->x,path->y)) { stepTaken(grid[x][y]->id,grid[path->x][path->y]->id); // printf("Step correct! (from %d to %d)\n", grid[x][y]->id,grid[path->x][path->y]->id);getchar(); x = path->x; y = path->y; path = path->next; addToEndOfPath(&robot_path, x, y); } else { if (antiMine) goto frombeginning; removeConnection(x,y,path->x,path->y); failed = 1; break; } cls_2(); printGrid(robot_path); } if (failed) { // Soooo it failed, boohoo. if (!intcpt) islandSN(grid[x][y]->id); //cls_2(); printf("Recalculating...\n"); combs = exploreArea(grid[x][y]->id); index = 0; cls_2(); printGrid(robot_path); continue; } // Okay now walk to second node path = findShortestPath(x, y, (path?path->facing_direction:NORTH), getNodeById(nodeTo)->positionGrid.x, getNodeById(nodeTo)->positionGrid.y); while (!(x == getNodeById(nodeTo)->positionGrid.x && y == getNodeById(nodeTo)->positionGrid.y)) { if (kbhit()) goto frombeginning; if (path && x == path->x && y == path->y) path = path->next; else if (!path) { printf("FAIL!\n"); getchar(); intcpt = 1; failed = 1; break; } if (makeMove(x,y,path->x,path->y)) { stepTaken(grid[x][y]->id,grid[path->x][path->y]->id); // printf("Step correct! (from %d to %d)\n", grid[x][y]->id,grid[path->x][path->y]->id);getchar(); x = path->x; y = path->y; path = path->next; addToEndOfPath(&robot_path, x, y); } else { if (antiMine) goto frombeginning; removeConnection(x,y,path->x,path->y); failed = 1; break; } cls_2(); printGrid(robot_path); } if (failed) { // Soooo it failed, boohoo. if (!intcpt) islandSN(grid[x][y]->id); //cls_2(); printf("Recalculating...\n"); combs = exploreArea(grid[x][y]->id); index = 0; // islandSN(grid[x][y]->id); cls_2(); printGrid(robot_path); continue; } // Guess everything went okay index += 2; } printf("Aaaaaaaaaaaaaand we're done, type '1' to rerun and '5' to rerun in anti-mine mode!\n"); key = 0; while (key==0) { scanf("%d",&key); } if (key==1) goto rerun; else if(key==5) goto antimine; #endif return EXIT_SUCCESS; }
/** * Removed disjunct parts of the grid * @param n */ void islandSN(int n) { resetSN(); exploreSN(n,-1,-1); int i, num = 0; for (i = 0; i < NUM_NODES; i++) { if (!inSeenNodes(i)) { num++; if (getNodeById(i)->north) { if (getNodeById(getNodeById(i)->north->id)->south) { getNodeById(getNodeById(i)->north->id)->south = NULL; } getNodeById(i)->north = NULL; } if (getNodeById(i)->south) { if (getNodeById(getNodeById(i)->south->id)->north) { getNodeById(getNodeById(i)->south->id)->north = NULL; } getNodeById(i)->south = NULL; } if (getNodeById(i)->west) { if (getNodeById(getNodeById(i)->west->id)->east) { getNodeById(getNodeById(i)->west->id)->east = NULL; } getNodeById(i)->west = NULL; } if (getNodeById(i)->east) { if (getNodeById(getNodeById(i)->east->id)->west) { getNodeById(getNodeById(i)->east->id)->west = NULL; } getNodeById(i)->east = NULL; } } } printf("Disjunct nodes: %d\n",num); if (num==24) { printf("Something went wrong somewhere else!\n"); getchar(); } }
/** * Finds the attached nodes * @param node * @param n1 * @param n2 */ void exploreSN(int node, int n1, int n2) { if (!inSeenNodes(node)) { // seenNodes[seenNodesSize++] = seenNodes[seenNodesSize++] = node; } if (getNodeById(node)->north && !inSeenNodes(getNodeById(node)->north->id)) exploreSN(getNodeById(node)->north->id,n1,n2); if (getNodeById(node)->south && !inSeenNodes(getNodeById(node)->south->id)) exploreSN(getNodeById(node)->south->id,n1,n2); if (getNodeById(node)->west && !inSeenNodes(getNodeById(node)->west->id)) exploreSN(getNodeById(node)->west->id,n1,n2); if (getNodeById(node)->east && !inSeenNodes(getNodeById(node)->east->id)) exploreSN(getNodeById(node)->east->id,n1,n2); }