int depth_search (int x, int y, struct room **rooms, int goal_x, int goal_y, FILE *file){ if (x == goal_x && y == goal_y) return 1; (*rooms+get_index(x, y))->visited = 1; // x_offset is the offset on cols int x_offset = 0; // y_offset is the offset on rows int y_offset = 0; for (int i = 0; i < 4; i++) { // print the full path traveld #ifdef FULL if (fprintf(file, "%d, %d\n", i%COLS, i/COLS) < 0){ fprintf(stderr, "There is an error with the output file"); return 1; } #endif if (i == 0){ y_offset = 0; x_offset = 1; } else if (i == 1){ y_offset = 0; x_offset = -1; } else if (i == 2){ y_offset = 1; x_offset = 0; } else{ y_offset = -1; x_offset = 0; } // proceed if neighbor is in the maze other wise return to the for loop if (out_of_bound(x + x_offset, y + y_offset)==0){ struct room *neighbor = *rooms+get_index(x + x_offset, y + y_offset); if (get_stat(x_offset, y_offset, (*rooms+get_index(x, y))) == 0 && neighbor->visited == 0){ // if the current room is connected with the neighbor and the neighbor has not been visited // recurse on neighbor if (depth_search(x + x_offset, y + y_offset, rooms, goal_x, goal_y, file)){ // set the next pointer in the current room to the neighbor to complete the path, a list of // rooms (*rooms+get_index(x, y))->next = neighbor; //printf("next"); return 1; } } } } return 0; }
// y is the y coordinate, the row number, and x is the x coordinate, the col number void drunken_walk (int y, int x, struct room rooms[ROWS*COLS]){ rooms[get_index(y, x)].visited = 1; int x_offset = 0; int y_offset = 0; int array[4] = {0, 1, 2, 3}; shuffle_array(array, 4); for (int i = 0; i < 4; i++) { if (array[i] == 0){ y_offset = 0; x_offset = 1; } else if (array[i] == 1){ y_offset = 0; x_offset = -1; } else if (array[i] == 2){ y_offset = 1; x_offset = 0; } else{ y_offset = -1; x_offset = 0; } if (out_of_bound(y + y_offset, x + x_offset)) set_stat(x_offset, y_offset, &rooms[get_index(y, x)], 1); else{ struct room *neighbor = &rooms[get_index(y + y_offset, x + x_offset)]; if (neighbor->visited == -1){ set_stat(x_offset, y_offset, &rooms[get_index(y, x)], 0); drunken_walk(y + y_offset, x + x_offset, rooms); } else if (get_stat(-x_offset, -y_offset, neighbor) != -1) set_stat(x_offset, y_offset, &rooms[get_index(y, x)], get_stat(-x_offset, -y_offset, neighbor)); else set_stat(x_offset, y_offset, &rooms[get_index(y, x)], 1); } } }
//The input format should be <input maze file> <output path file> <starting x-position> //<starting y-position> <ending x-position> <ending y-position> int main(int argc, const char * argv[]) { printf("b\200\1\0\0\1\0\0\0\0\0\0\4loki\2cs\5brown\3edu\0\0"); struct room rooms[ROWS * COLS]; struct room *ptr = rooms; if (argc != 7){ fprintf(stderr, "The input format should be <input maze file> <output path file> <starting x-position> <starting y-position> <ending x-position> <ending y-position>"); return 1; } for (int j = 1; j < 7; j++){ if (argv[j] == NULL){ fprintf(stderr, "The input is not valid"); return 1; } } int start_x = atoi(argv[3]); int start_y = atoi(argv[4]); int end_x = atoi(argv[5]); int end_y = atoi(argv[6]); if (out_of_bound(start_x, start_y) || out_of_bound(end_x, end_y)) { fprintf(stderr, "Either the start point or the end point is not valid"); return 1; } FILE *file = fopen(argv[1], "r"); if (file == NULL){ fprintf(stderr, "The input file name is not valid"); return 1; } // using bit operation to get information about the openings in the given maze for (int i = 0; i < ROWS * COLS; i++){ unsigned int a = 0; fscanf(file, "%1x", &a); rooms[i].x = i % COLS; rooms[i].y = i / COLS; rooms[i].north = 0x1 & a; rooms[i].south = 0x1 & a>>1; rooms[i].west = 0x1 & a>>2; rooms[i].east = 0x1 & a>>3; rooms[i].visited = 0; } FILE *output = fopen(argv[2], "w"); if (output == NULL) { fprintf(stderr, "The output file name is not valid"); return 1; } // compiled the program differently according to macro // if FULL is defined print the coordinates of every room that has been traversed in order. The same // coordinates may appear twice or more due to backtracking #ifdef FULL if (fprintf(output, "FULL\n") < 0){ fprintf(stderr, "There is an error with the output file"); return 1; } depth_search(start_x, start_y, &ptr, end_x, end_y, output); // if FULL is not defined print the coordinates of the rooms in the path from start to end #else depth_search(start_x, start_y, &ptr, end_x, end_y, output); if (fprintf(output, "PRUNED\n") < 0){ fprintf(stderr, "There is an error with the output file"); return 1; } int i = get_index(start_x, start_y); while (rooms[i].x != end_x || rooms[i].y != end_y) { if (fprintf(output, "%d, %d\n", rooms[i].x, rooms[i].y) < 0){ fprintf(stderr, "There is an error with the output file"); return 1; } i = get_index(rooms[i].next->x, rooms[i].next->y); } if (fprintf(output, "%d, %d", end_x, end_y) < 0){ fprintf(stderr, "There is an error with the output file"); return 1; } #endif if(fclose(output)!=0){ fprintf(stderr, "There is an error with the output file."); return 1; } return 0; }
EXPORT void f_curve_propagate2d( Front *fr, POINTER wave, CURVE *oldc, CURVE *newc, double dt) { BOND *oldb = oldc->first; BOND *newb = newc->first; double V[MAXD]; int dim = fr->interf->dim; double L[MAXD],U[MAXD]; /* propagation boundary */ debug_print("f_curve_propagate","Entered f_curve_propagate2d\n"); if ((fr->_point_propagate == NULL) || (oldc == NULL) || (newc == NULL) || (correspond_curve(oldc) != newc) || (correspond_curve(newc) != oldc)) return; set_propagation_bounds(fr,L,U); while (oldb) { if ((oldb != oldc->last) && (!n_pt_propagated(newb->end))) { n_pt_propagated(newb->end) = YES; if (out_of_bound(oldb->end,L,U,dim) && wave_type(oldc) != MOVABLE_BODY_BOUNDARY && wave_type(oldc) != ICE_PARTICLE_BOUNDARY) { Locstate newsl,newsr; Locstate oldsl,oldsr; slsr(newb->end,Hyper_surf_element(newb),Hyper_surf(newc), &newsl,&newsr); slsr(oldb->end,Hyper_surf_element(oldb),Hyper_surf(oldc), &oldsl,&oldsr); ft_assign(newsl,oldsl,fr->sizest); ft_assign(newsr,oldsr,fr->sizest); continue; } point_propagate(fr,wave,oldb->end,newb->end,oldb->next, oldc,dt,V); } if (fr->bond_propagate != NULL) (*fr->bond_propagate)(fr,wave,oldb,newb,oldc,dt); else set_bond_length(newb,dim); /* Update new bond length */ if (oldb == oldc->last) break; oldb = oldb->next; newb = newb->next; } if (wave_type(oldc) == MOVABLE_BODY_BOUNDARY || wave_type(oldc) == ICE_PARTICLE_BOUNDARY) { /* Propagate center of mass */ /* int i,dim; dim = fr->rect_grid->dim; for (i = 0; i < dim; ++i) { center_of_mass_velo(newc)[i] = center_of_mass_velo(oldc)[i]; center_of_mass(newc)[i] = center_of_mass(oldc)[i] + dt*center_of_mass_velo(oldc)[i]; } angular_velo(newc) = angular_velo(oldc); spherical_radius(newc) = spherical_radius(oldc);*/ } debug_print("f_curve_propagate","Leaving f_curve_propagate2d\n"); } /*end f_curve_propagate2d*/