Computer Review questions (C language edition)
Introduction
The project will read flight data from an input file andflight path requests from another input file and output the requiredinformation.
?
Your Task
Your program shocould determineIf aparticular destination airport can be reached from a particle originatingairport within a particle number of hops.
A hop (leg of a flight) is a flight from one airport to anotheron the path between an originating and destination airports.
For example, the flight plan from PVG to PEK might be PVG → CAN → PEK. So PVG → CAN wocould be a hop and CAN → PEK wocould be a hop.
?
Input Data Files
Path InputFile(PathInput.txt)
This input file will consist of a number of single origination/destinationairport pairs (direct flights ). the first line of the file will contain in aninteger representing the total number of pairs in the rest of the file.
6 [PVG, CAN] [CAN, PEK] [PVG, CTU] [CTU, DLC] [DLC, HAK] [HAK, LXA] |
?
PathRequest File(PathRequest.txt)
This input file will contain in a sequence of pairs oforigination/destination airports and a max number of hops. The first line ofthe file will contain in an integer representing the number of pairs in the file.
2 [PVG, DLC, 2] [PVG, LXA, 2] |
?
OutputFile(Output.txt)
For each pair in the Path Request File, your programshocould output the pair followed by "YES" or "NO" indicating thatIt is possible to get from the origination to destinationairports within the max number of hops or it is not possible, Respectively.
[PVG, DLC, YES] [PVG, LXA, NO] |
Assumptions youcan make:
You may make the following simplifying assumptions inyour project:
L? C/C ++ is allowed to be used.
L? All airport codes will be 3 letters and will be in allcaps
L? Origination/destination pairs are unidirectional. toindicate that both directions of flight are possible, two entries wowould appearin the file. for example, [PVG, PEK] and [PEK, PVG] wocould have to be present inthe file to indicate that one cocould fly from Shanghaito Beijing and from Beijingto Shanghai.
Meaning understanding:
Given a graph, any two points in the input graph, whether the two points can be connected within the given maximum number of hops
For example
[PVG, DLC, 2] The given maximum hop count is 2, and the actual hop count is 2. The maximum hop count output is YES.
[PVG, LXA, 2]The maximum number of hops is 2, and the actual number of hops is 4. the maximum number of hops is output NO.
Because it does not directly read from the file
6
[PVG, CAN]
[CAN, PEK]
[PVG, CTU]
[CTU, DLC]
[DLC, HAK]
[HAK, LXA]
2
[PVG, DLC, 2]
[PVG, LXA, 2]
Such data
Therefore, you can only simulate the input status after the file is read:
6
PVG CAN
CAN PEK
PVG CTU
CTU DLC
DLC HAK
HAK LXA
3
Pvg dlc 2
Pvg lxa 2
Output result
[PVG, DLC, YES]
[PVG, LXA, NO]
This question algorithm is not difficult to be a DFS, that is, the graph node is a character, a bit pitfall
The following is a program, not necessarily true. How to use the program
Input data
6
PVG CAN
CAN PEK
PVG CTU
CTU DLC
DLC HAK
HAK LXA
3
Pvg dlc 2
Pvg lxa 2
Output Data
[PVG, DLC, YES]
[PVG, LXA, NO]
#include
#include
struct nodeinfo
{
char no [10]; // the name of the node;
char joint [100] [10]; // The set of all nodes connected to the node
int num_edge = 0; // degree of node
int visited; // Visit flag
} node [500];
int num_node = 0, flag; // num_node is the number of nodes in the graph
int ni (char a []) // Find the position of the character node in the structure array
{
for (int i = 0; i <num_node; i ++) if (! strcmp (node [i] .no, a)) = "" return = "" i; = "" -1; = "" -1 = ""} = "" bool = "" islink (char = "" a [], char = "" b []) = "" determine whether the two nodes are connected = "" {= "" int = "" i = "ni (a);" for (int = "" j = "0; j