Deep Search Solution Riding the Fences

Source: Internet
Author: User

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 the them (and nowhere else) and fixing the broken parts.

Farmer John is as a lazy as the next Farmer and hates to ride the samefence twice. Your program must read in a description of a network offences and the Farmer John a path to traverse each fence length ex Actlyonce, if possible. Farmer J can, if he wishes, start and finish at Anyfence intersection.

Every fence connects, fence intersections, which is numberedinclusively from 1 through (though some farms has 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 is "connected").

Your program must output the path of intersections that, if Interpretedas a base number, would has the smallest Magni Tude.

There always is at the least one solution for each set of the 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's 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 many possible answers toany given input set, but only one is ordered correctly.

SAMPLE OUTPUT (file fence.out)
1234254657

Solution Solutions

Find the starting point, and then use the deep search from the starting point to find the Euler path, the only thing to note is that there may be multiple side connections between the two points.


Run results

   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]< C3/>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.

by code

/*id:c1033311lang:c++task:fence*/#include <stdio.h> #include <string.h> #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<MAX;++V) {if (G[u][v]) {g[u][v]- -; G[v][u]--;euler (v);p Oint[n++]=v;}}} int main () {FILE *fin=fopen ("fence.in", "R"); FILE *fout=fopen ("Fence.out", "w"), int f,i,a,b;int start;fscanf (FIN, "%d", &f);    Enter for (i=0;i<f;++i) {fscanf (Fin, "%d%d", &a,&b); g[a][b]++;    There may be more than one edge between two points g[b][a]++;d eg[a]++;d eg[b]++;} Start=1;for (i=1;i<max;++i)     //Search for starting if (deg[i]%2) {start=i;break;} Euler (start);   Look for the Euler path Point[f]=start;for (i=f;i>=0;--i)   //Output fprintf (Fout, "%d\n", Point[i]); return 0;}


Deep Search Solution Riding the Fences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.