Usaco wormholes (Analog)

Source: Internet
Author: User

Please point me in the question.
The following:
The idea of this problem is simple, simple deep search, find all the combination, and then determine whether the ring. The key is how to determine whether the ring, my idea is to use recursive simulation, see whether the second pass a certain point. There is also an error in the middle, first, the next point of each visit should be the first point to the right of the current point on the same line, and secondly, a point that has been accessed must be accessed as a starting point, not just to arrive.
Code implementation:

/*id:eashionlang:c++task:wormhole*/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#define MAX#define INF 0x7fffffffusing namespace STD;structnode{intx, y;};intNintResintUsed[max];//Pairing informationintPair[max];//Point setNode Nlis[max];//test whether to ringBOOLTest ();//debug printout ResultsvoidPrint ();voidDfsintPOS);//RecursiveBOOLIf_sucked (intPOS);intMain () {Freopen ("Wormhole.in","R", stdin); Freopen ("Wormhole.out","W", stdout); while(scanf("%d", &n)! = EOF) {res =0;memset(Used,0,sizeof(used));memset(pair,-1,sizeof(Pair)); for(inti =0; i < N; i++) {scanf("%d%d", &AMP;NLIS[I].X,&AMP;NLIS[I].Y); } DFS (0);printf("%d\n", res); }return 0;}voidDfsintPOS) {if(pos = = N) {if(Test ()) {res++;//print ();            //Quick debug, positioning to fit understanding and then testing again            //test ();}return; }if(Pair[pos] = =-1){ for(inti = pos+1; i < N; i++) {if(Pair[i] = =-1) {Pair[i] = pos;                Pair[pos] = i; DFS (pos+1); Pair[i] =-1; Pair[pos] =-1; }        }    }Else{DFS (pos+1); }return;}BOOLTest () { for(inti =0; i < N; i++) {memset(Used,0,sizeof(used));if(If_sucked (i)) {return true; }    }return false;}BOOLIf_sucked (intPOS) {BOOLFlag =false;//Here only mark the beginning of each time is visitedUsed[pos] =1;//used[pair[pos]] = 1;    intx = nlis[pair[pos]].x;inty = nlis[pair[pos]].y;intdis = INF;intin =-1;//Find the first point on the right     for(inti =0; i < N; i++) {if(nlis[i].x > x && nlis[i].y = = y) {if(Nlis[i].x-x < dis)                {dis = nlis[i].x-x;            in = i; }        }    }if(In! =-1){if(Used[in] = =1) {flag =true; }Else{flag = if_sucked (in); }    }returnFlag;}voidPrint () { for(inti =0; i < N; i++) {printf("%d%d\n", I,pair[i]); }printf("\ n");}

There are also routines on the solution, very ingenious, similar to and look at the set of feelings.

#include <iostream>#include <fstream>using namespace STD;#define MAX_NintN, x[max_n+1], y[max_n+1];intpartner[max_n+1];intnext_on_right[max_n+1];BOOLCycle_exists (void){ for(intstart=1; start<=n; start++) {//Does there exist a cylce starting from start    intpos = start; for(intCount=0; count<n; count++) pos = Next_on_right[partner[pos]];if(POS! =0)return true; }return false;}//Count All solutionsintSolvevoid) {//Find first unpaired wormhole  intI, total=0; for(i=1; i<=n; i++)if(Partner[i] = =0) Break;//Everyone paired?  if(i > N) {if(Cycle_exists ())return 1;Else return 0; }//Try pairing I with all possible other wormholes J   for(intj=i+1; j<=n; J + +)if(Partner[j] = =0) {//Try pairing I & J, let recursion continue to      //Generate the rest of the solutionPartner[i] = j;      PARTNER[J] = i;      Total + = solve (); Partner[i] = partner[j] =0; }returnTotal;}intMainvoid) {Ifstream fin ("Wormhole.in"); Fin >> N; for(intI=1; i<=n;  i++) fin >> x[i] >> y[i]; Fin.close (); for(intI=1; i<=n; i++)//Set Next_on_right[i] ...     for(intj=1; j<=n; J + +)if(X[j] > X[i] && y[i] = y[j])//J right of I ...    if(Next_on_right[i] = =0||  X[j]-x[i] < X[next_on_right[i]]-x[i]) next_on_right[i] = j; Ofstream Fout ("Wormhole.out"); Fout << Solve () <<"\ n"; Fout.close ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Usaco wormholes (Analog)

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.