[Usaco] wormholes [brute force]

Source: Internet
Author: User

Question: Give 2 k points on the plane, and pair them one by one. Ask how many pairing Methods There are to keep moving from a certain point to the right in the loop (k <= 6)

Idea: Because K is very small, the matching method is violent enumeration, and then ring judgment, when ring judgment, you need to pay attention to the four points 1, 2, 3, 1 and 3 on a straight line, pairing 2 and 4, you can find that it does not constitute a ring. For details, see the code.

 

/*{

ID: a4298442

Prob: Wormhole

Lang: C ++

}

*/

# Include <stdio. h>

# Include <iostream>

# Include <fstream>

# Include <string. h>

# Include <algorithm>

# Define maxn1000

Using namespace STD;

Ifstream fin ("wormhole. In ");

Ofstream fout ("wormhole. Out ");

Struct t {int X; int y;} p [maxn];

Int ans = 0, G [maxn], Match [maxn], n, visit [maxn];

Int CMP (t x, t y ){

Return (x. y <Y. Y | (x. y = Y. Y) & (x. x <Y. X )));

}

Int circle (int u)

{

Int vis [100] = {0}, V = G [u];

Vis [u] = 1;

While (V! = 0 ){

U = match [v];

If (! U) return 0; V = G [u];

If (vis [u]) return 1; vis [u] = 1;

}

Return 0;

}

Void DFS (int K, int U, int num ){

If (num = N ){

For (INT I = 1; I <= N; I ++) if (Circle (I) {ans ++; break ;}

Return;

}

Visit [k] = 1;

For (INT I = (u = 0 )? K + 1:1; I <= N; I ++) if (visit [I] = 0 ){

If (u = 0) {G [I] = K; G [k] = I ;}

DFS (I, u ^ 1, num + 1 );

If (u = 0) g [I] = G [k] = 0; else break;

}

Visit [k] = 0;

}

Int main (){

Fin> N;

For (INT I = 1; I <= N; I ++) Fin> P [I]. x> P [I]. Y;

Sort (p + 1, p + 1 + N, CMP );

For (INT I = 2; I <= N; I ++) if (P [I]. Y = P [I-1]. Y) Match [I-1] = I;

DFS (1, 0, 1 );

Fout <ans <Endl;

Return 0;

}

[Usaco] wormholes [brute force]

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.