/* Campus tour guide program [Problem description] Use the undirected network to represent the plan of the school's campus attractions. The vertices in the chart represent the main scenic spots and store the numbers, names, and descriptions of the scenic spots, The edges in the figure indicate the roads between scenic spots and the path length. You are required to be able to answer questions about the scenic spots and the tour path. Game You can ask through the terminal: (1) The shortest path from one scene to another. (Shortest path problem) (2) tourists enter from the park and select an optimal route. (3) allow visitors to browse all the scenic spots without repeat and return to the exit (the exit is next to the entrance ). [Basic requirements] (1) view the tour guide as a weighted undirected graph. The vertex represents the scenic spots of the park, the road between scenic spots, and the weight on the edge. Distance. select an appropriate data structure for this graph. (2) show all the routes to visitors and select their own routes. (3) draw a map of scenic spots on the screen. [Implementation tips] (1) construct an undirected graph G and store it with an adjacent matrix. (2) The dijela algorithm is used to calculate the shortest path from the start point to each vertex. The two-dimensional array P [I] [] is used to record the shortest path length. Degrees are stored in a one-dimensional array d [I]; I ranges from 0 ~ 20. (3) the one-dimensional array have [] is used to record the order in which the shortest path appears vertex. (4) output the shortest path and Path Length Based on the start and end points. */ # Include "string. H" # Include "stdio. H" # Include "stdio. H" # Include "malloc. H" # Include "stdlib. H" # Deprecision Max 20000 # Define num 9 Typedef struct arccell { Int adj;/* distance between adjacent scenic spots */ } Arccell;/* defines the edge type */ Typedef struct vertextype { Int number;/* scenic spot number */ Char * sight;/* scenic spot name */ Char * description;/* description of scenic spots */ } Vertextype;/* defines the vertex type */ Typedef struct { Vertextype Vex [num];/* vertices in the graph, that is, scenic spots */ Arccell arcs [num] [num];/* edge in the figure, that is, the distance between scenic spots */ Int vexnum, arcnum;/* Number of vertices, number of edges */ } Mgraph;/* define the graph type */ Mgraph g;/* define the graph as a global variable */ Int P [num] [num];/**/ Long int d [num];/* minimum path length of auxiliary variable storage */ Int X [9] = {0 }; Void createudn (int v, int A);/* graph creation function */ Void narrate ();/* description function */ Void shortestpath (INT num);/* Shortest Path function */ Void output (INT sight1, int sight2);/* output function */ Char menu ();/* Main Menu */ Void search ();/* query scenic spot information */ Char searchmenu ();/* query sub menu */ Void Hamitonian (INT);/* traverse the Hamilton Graph */ Void nextvalue (INT ); Void display ();/* display the traversal result */ Void main ()/* main function */ { Int v0, V1; Char CK; System ("color FC "); Createudn (Num, 11 ); Do { Ck = menu (); Switch (CK) { Case '1 ': System ("CLS "); Narrate (); Printf ("/n/T, select the starting point (0 ~ 8 ):"); Scanf ("% d", & V0 ); Printf ("/T please select the destination attraction (0 ~ 8 ):"); Scanf ("% d", & V1 ); Shortestpath (v0);/* calculate the shortest path between two scenic spots */ Output (v0, V1);/* output result */ Printf ("/n/T press any key to continue.../N "); Getchar (); Getchar (); Break; Case '2': Search (); Break; Case '3 ': System ("CLS "); Narrate (); X [0] = 1; Hamitonian (1 ); Printf ("/n/T press any key to continue.../N "); Getchar (); Getchar (); Break; }; } While (CK! = 'E '); } Char menu ()/* Main Menu */ { Char C; Int flag; Do { Flag = 1; System ("CLS "); Narrate (); Printf ("/n/T ┏ ━━ ━━━ ━━ ━━n n/n "); Printf ("/T others/N "); Printf ("/T ┃ 1. query the scenic spots ┃/N "); Printf ("/T ┃ 2. query scenic spot information ┃/N "); Printf ("/T ┃ 3. Recommended Route ┃/N "); Printf ("/T ┃ E, quit/N "); Printf ("/T others/N "); Printf ("/T ┗ ━━ ━━━ ━━ ━ ━n n/n "); Printf ("/T please input your choice :"); Scanf ("% C", & C ); If (C = '1' | C = '2' | C = '3' | C = 'E ') Flag = 0; } While (FLAG ); Return C; }Char searchmenu ()/* query sub menu */ { Char C; Int flag; Do { Flag = 1; System ("CLS "); Narrate (); Printf ("/n/T ┏ ━━ ━━━ ━━ ━━n n/n "); Printf ("/T others/N "); Printf ("/T ┃ 1. query region/N by scenic spot number "); Printf ("/T ┃ 2. query region/N by scenic spot name "); Printf ("/T ┃ E, return Bytes/N "); Printf ("/T others/N "); Printf ("/T ┗ ━━ ━━━ ━━ ━ ━n n/n "); Printf ("/T please input your choice :"); Scanf ("% C", & C ); If (C = '1' | C = '2' | C = 'E ') Flag = 0; } While (FLAG ); Return C; } Void search ()/* query scenic spot information */ { Int num; Int I; Char C; Char name [20]; Do { System ("CLS "); C = searchmenu (); Switch (c) { Case '1 ': System ("CLS "); Narrate (); Printf ("/n/T, enter the ID of the attraction you want to find :"); Scanf ("% d", & num ); For (I = 0; I <num; I ++) { If (num = G. Vex [I]. Number) { Printf ("/n/T :"); Printf ("/n/T %-25 s/n", G. Vex [I]. Description ); Printf ("/n/T press any key to return ..."); Getchar (); Getchar (); Break; } } If (I = num) { Printf ("/n/T not found! "); Printf ("/n/T press any key to return ..."); Getchar (); Getchar (); }
Break; Case '2 ': System ("CLS "); Narrate (); Printf ("/n/T, enter the name of the attraction you want to find :"); Scanf ("% s", name ); For (I = 0; I <num; I ++) { If (! Strcmp (name, G. Vex [I]. Sight )) { Printf ("/n/T :"); Printf ("/n/T %-25 s/n", G. Vex [I]. Description ); Printf ("/n/T press any key to return ..."); Getchar (); Getchar (); Break; } } If (I = num) { Printf ("/n/T not found! "); Printf ("/n/T press any key to return ..."); Getchar (); Getchar (); } Break; } } While (C! = 'E '); } Void createudn (int v, int A)/* graph creation function */ { Int I, J; G. vexnum = V;/* Number of scenic spots and edges in the initialization structure */ G. arcnum =; For (I = 0; I <G. vexnum; ++ I) g. Vex [I]. Number = I;/* initialize the ID of each Scenic Spot */ /* Initialize none of the scenic spots and their descriptions */ G. Vex [0]. Sight = ""; G. Vex [0]. Description = "school leader, home of the Office. "; G. Vex [1]. Sight = "auditorium "; G. Vex [1]. Description = "in your spare time, hold various parties. "; G. Vex [2]. Sight = "Comprehensive Building "; G. Vex [2]. Description = "classroom "; G. Vex [3]. Sight = "nanjiao "; G. Vex [3]. Description = "classroom "; G. Vex [4]. Sight = "Qinyuan "; G. Vex [4]. Description = "Garden "; G. Vex [5]. Sight = ""; G. Vex [5]. Description = "Garden "; G. Vex [6]. Sight = "beijiao "; G. Vex [6]. Description = "classroom "; G. Vex [7]. Sight = "library "; G. Vex [7]. Description = "borrow and return books "; G. Vex [8]. Sight = "Bishop "; G. Vex [8]. Description = "Main Teaching Building "; /* All edges are assumed to be 20000, meaning that the two scenic spots cannot be reached */ For (I = 0; I <G. vexnum; ++ I) For (j = 0; j <G. vexnum; ++ J) G. arcs [I] [J]. adj = max;
/*
Below is the distance between directly accessible scenic spots, because the distance between the two scenic spots is mutual, Therefore, you must assign values to symmetric edges in the graph at the same time.
*/ G. arcs [0] [1]. adj = G. arcs [1] [0]. adj = 6; G. arcs [0] [2]. adj = G. arcs [2] [0]. adj = 4; G. arcs [0] [3]. adj = G. arcs [3] [0]. adj = 5; G. arcs [1] [4]. adj = G. arcs [4] [1]. adj = 1; G. arcs [2] [4]. adj = G. arcs [4] [2]. adj = 1; G. arcs [3] [5]. adj = G. arcs [5] [3]. adj = 2; G. arcs [5] [7]. adj = G. arcs [7] [5]. adj = 4; G. arcs [4] [6]. adj = G. arcs [6] [4]. adj = 9; G. arcs [4] [7]. adj = G. arcs [7] [4]. adj = 7; G. arcs [6] [8]. adj = G. arcs [8] [6]. adj = 2; G. arcs [7] [8]. adj = G. arcs [8] [7]. adj = 4; } Void narrate ()/* description function */ { Int I, K = 0; Printf ("/n/T ***************** welcome to the campus tour guide program ********* * *****/N "); Printf ("/T __________________________________________________________________/N "); Printf ("/T scenic spot name/T |/T Scenic Spot description/N "); Printf ("/T ________________________________ | _________________________________/N "); For (I = 0; I <num; I ++) { Printf ("/T % C (% 2d) %-10 s/t/T |/T %-25 S % C/N", 3, I, G. vex [I]. sight, G. vex [I]. description, 3);/* List of output scenic spots */ K = k + 1; } Printf ("/T ________________________________ | _________________________________/N "); } Void shortestpath (INT num)/* dijela algorithm shortestpath function num is the number of the entry point */ { Int V, W, I, T;/* I, W, and V are the counting variables */ Int final [num];/**/ Int min; For (V = 0; v <num; V ++) { Final [v] = 0;/* assume that there is no shortest path from vertex num to vertex v */ D [v] = G. arcs [num] [v]. adj;/* store the relevant weights in D */ For (W = 0; W <num; W ++)/* set to null path */ P [v] [W] = 0; If (d [v] <20000)/* path of existence */ { P [v] [num] = 1;/* set the existence flag */ P [v] [v] = 1;/* itself */ } } D [num] = 0; Final [num] = 1;/* initialize the num vertex to belong to the s set */ /* Start the main loop, obtain the shortest path from num to a vertex each time, and add it to the s set */ For (I = 0; I <num; ++ I)/* Other G. vexnum-1 vertices */ { Min = max;/* the closest known distance to the vertex num */ For (W = 0; W <num; ++ W) If (! Final [w])/* w vertex in V-S */ If (d [w] <min)/* w vertices are closer to num vertices */ { V = W; Min = d [w]; } Final [v] = 1;/* Add v closer to the num vertex to the s set */ For (W = 0; W <num; ++ W)/* update the current Shortest Path */ If (! Final [w] & (min + G. ARCs [v] [W]. adj) <D [w])/* The current path is not in the s set and is shorter than the previously found path */ { D [w] = min + G. arcs [v] [W]. adj; For (t = 0; t <num; t ++) P [w] [T] = P [v] [T]; P [w] [W] = 1; } } } Void output (INT sight1, int sight2)/* output function */ { Int A, B, C, D, q = 0; A = sight2;/* assign attraction 2 to */ If (! = Sight1)/* If attraction 2 does not overlap with attraction 1, then ...*/ { Printf ("/n/t the shortest path from % s to % s is", G. vex [sight1]. sight, G. vex [sight2]. sight);/* output prompt information */ Printf ("/t (the shortest distance is % DM .) /n/T ", d [a]);/* output the shortest path length from sight1 to sight2, stored in the d [] array */ Printf ("/T % s", G. Vex [sight1]. Sight);/* Name of attraction 1 Output */ D = sight1;/* assign the ID of attraction 1 to D */ For (C = 0; C <num; ++ C) { Gate:;/* number, which can be used as the position to jump to a GOTO statement */ P [a] [sight1] = 0; For (B = 0; B <num; B ++) { If (G. ARCs [d] [B]. adj <20000 & P [a] [B])/* if there is a path and a shortest path between an attraction and its critical point */ { Printf ("--> % s", G. Vex [B]. Sight);/* output the name of this node */ Q = q + 1;/* Add one to the counting variable, and wrap the output when the output is full 8 */ P [a] [B] = 0; D = B;/* use B as the starting point for the next loop output, so repeated */ If (Q % 8 = 0) printf ("/N "); Goto gate; } } } } } Void Hamitonian (int m)/* traverse the Hamilton Graph */ { If (M> 8) return; L: nextvalue (m ); If (X [m] = 0) Return; If (M = 7 & G. arcs [0] [x [8]-1]. adj! = 20000) Display (); Else Hamitonian (m + 1 ); Goto L; } Void nextvalue (int K) { Int J; L: X [k] = (X [k] + 1) % 10; If (X [k] = 0) Return; If (G. arcs [x [k-1]-1] [x [k]-1]. adj! = 20000) { For (j = 0; j <K; j ++) If (X [J] = x [k]) Goto L; Return; } Else Goto L; } Void display () { Int I = 0; Printf ("/n/t "); For (I = 0; I <8; I ++) Printf ("% s->", G. Vex [x [I]-1]. Sight ); Printf ("exit "); Printf ("/N "); } |