void depth_search(tree_node *r, int **f, int *next_resv, int resv, int left_resv, int prev = 0) {//处理对当前r的所有孩子节点的分配工作 //给节点r分配的剩余节点为resv个 //给r的所有孩子节点分配的节点存储在next_resv数组中 //将r的孩子树们分配的所有情况中的最大权值挑选出来赋给f[r->t_idx][resv] //只处理当前r节点,不会处理到下一层的节点 //递归结束条件,深度搜索出一种组合后进行计算 if(prev == r->t_cnt){ //更新r树的最大权值 int sumf(0); //下一层子树的最大权值又交给另一个递归去做 for(int i = 0; i < r->t_cnt; ++ i) sumf += multi_tree(r->t_child[i], f, next_resv[i]); f[r->t_idx][resv] = max(f[r->t_idx][resv], sumf); return; } //对r的一个指定孩子prev if(prev == r->t_cnt - 1){ //如果prev是孩子节点中的最后一个,它直接分配到剩余的所有节点 next_resv[prev] = left_resv; depth_search(r, f, next_resv, resv, 0, prev + 1); } else{ //对于当前孩子prev,它可以分配的节点数从 0 到 left_resv for(int i = 0; i <= left_resv; ++ i){ //当前孩子分配 i 个节点 next_resv[prev] = i; //下一个孩子 prev+1 只能分配 left_resv-i 个节点 depth_search(r, f, next_resv, resv, left_resv - i, prev + 1); } } }
int main(int argc, char *argv[]) { int max_depth = atoi(argv[2]); depth_search((char *)argv[1], (char *)argv[3], max_depth); printf("Can't find the file :(\n"); return 0; }
//_________________________________________________________________________ bool graph_molloy_hash::is_connected() { bool *visited = new bool[n]; int *buff = new int[n]; int comp_size = depth_search(visited, buff); delete[] visited; delete[] buff; return (comp_size==n); }
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; }
int multi_tree(tree_node *r, int **f, int resv) {//二叉树根节点为r,初始时f为0,保留resv个节点,返回最大权值 if(r == NULL) return(0); if(resv == 0) return(0); if(f[r->t_idx][resv]) return(f[r->t_idx][resv]); //这个next_resv中存储着r的孩子们分配节点的数量 int next_resv[MAX]; for(int i = 0; i < r->t_cnt; ++ i) next_resv[i] = 0; //要对当前节点r的所有孩子分配剩余的 resv-1 个节点(根节点自己必须留1个) //用深度搜索找出所有可能,并将最大的权值赋给f[r->t_idx][resv] depth_search(r, f, (int*)next_resv, resv, resv - 1, 0); f[r->t_idx][resv] += r->t_value; return(f[r->t_idx][resv]); }
void depth_search(char *direc_name, char *file, int depth) { struct stat object; struct dirent *buf; DIR *direc = opendir(direc_name); char *path = (char *)malloc(sizeof(char)); if (direc == NULL) { perror(NULL); exit(-1); } while ((buf = readdir(direc)) != NULL) { if (strcmp(buf->d_name, ".") != 0 && strcmp(buf->d_name, "..") != 0) { path = realloc(path, sizeof(char) * (strlen(direc_name) + strlen(buf->d_name) + 5)); strcpy(path, direc_name); strcat(path, "/"); strcat(path, buf->d_name); stat(path, &object); if (S_ISDIR(object.st_mode) && depth > 0) depth_search(path, file, depth - 1); else if (S_ISREG(object.st_mode) && strcmp(file, buf->d_name) == 0) { printf("Your file is found at %s\n", path); /* * Лучше стараться писать ф-и, которые потенциально можно будет ещё где-то применить. * Если убивается текущий процесс, то это уже вряд ли можно будет где-то ещё использовать. */ exit(0); } } } free(path); closedir(direc); }
//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; }