In-depth search for Riding the Fences
Riding the Fences
Farmer John owns a large number of fences that must be repairedannually. He traverses the fences by riding a horse along each andevery one of them (and nowhere else) and fixing the broken parts.
Farmer John is as lazy as the next farmer and hates to ride the samefence twice. your program must read in a description of a network offences and tell Farmer John a path to traverse each fence length exactlyonce, if possible. farmer J can, if he wishes, start and finish at anyfence intersection.
Every fence connects two fence intersections, which are numberedinclusively from 1 through 500 (though some farms have far fewer than500 intersections ). any number of fences (> = 1) can meet at a fenceintersection. it is always possible to ride from any fence to any otherfence (I. e ., all fences are "connected ").
Your program must output the path of intersections that, if interpretedas a base 500 number, wocould have the smallest magn.pdf.
There will always be at least one solution for each set of inputdata supplied to your program for testing.
Program name: fenceINPUT FORMAT
Line 1: |
The number of fences, F (1 <= F <= 1024) |
Line 2. F + 1: |
A pair of integers (1 <= I, j <= 500) that tell which pair of intersections this fence connects. |
Sample input (file fence. in)
91 22 33 44 24 52 55 65 74 6
OUTPUT FORMAT
The output consists of F + 1 lines, each containing a single integer. print the number of the starting intersection on the first line, thenext intersection's number on the next line, and so on, until the finalintersection on the last line. there might be empty possible answers toany given input set, but only one is ordered correctly.
Sample output (file fence. out)
1234254657
Solution
Find the starting point and then use deep search to find the Euler's path from the starting point. The only thing you need to note is that there may be multiple edge connections between two points.
Running result
Executing... Test 1: TEST OK [0.003 secs, 4480 KB] Test 2: TEST OK [0.003 secs, 4480 KB] Test 3: TEST OK [0.005 secs, 4480 KB] Test 4: TEST OK [0.003 secs, 4480 KB] Test 5: TEST OK [0.005 secs, 4480 KB] Test 6: TEST OK [0.008 secs, 4480 KB] Test 7: TEST OK [0.011 secs, 4480 KB] Test 8: TEST OK [0.014 secs, 4480 KB]All tests OK.
Code
/* ID: c1033311LANG: C ++ TASK: fence */# include
# Include
# Define MAX 501 # define MAXP 1030int point [MAXP], deg [MAX], G [MAX] [MAX]; int n = 0; void euler (int u) {int v; for (v = 1; v
= 0; -- I) // output fprintf (fout, "% d \ n", point [I]); return 0 ;}