其他分享
首页 > 其他分享> > 2021-01-04 USACO Wormholes

2021-01-04 USACO Wormholes

作者:互联网

Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).

According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.

For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
   | A > B .      Bessie will travel to B then
   + . . . .      A then across to B again

Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.

Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn't know which wormhole pairs with any other wormhole, so \fBfind all the possibilities (i.e., all the different ways that N wormholes could be paired such that Bessie can, in some way, get in a cycle\fP). Note that a loop with a smaller number of wormholes might contribute a number of different sets of pairings to the total count as those wormholes that are not in the loop are paired in many different ways.

PROGRAM NAME: wormhole

INPUT FORMAT:

Line 1:The number of wormholes, N.
Lines 2..1+N:Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT (file wormhole.in):

4
0 0
1 0
1 1
0 1

INPUT DETAILS:

There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT:

Line 1:The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT (file wormhole.out):

2

OUTPUT DETAILS:

If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . 
   d c . 
   a b . 

Here is a list of all the pairings, annotated with their wormhole results:
(a b) (c d) -- Bessie loops (a b) if she is on that line
(a c) (b d) -- Bessie loops b->d->c->a->b ... if she gets caught
(a d) (b c)

Only the pairings a-d and b-c allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling.

 

最初的想法是用组合数来直接算出来,但情况太多了,实在是没办法。最后看了题解都被震惊了,我花了这么多时间在这暴力可以解决的题上面。。。

/*
ID: traysen1
TASK: wormhole
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 20;
int N;
int x[MAXN], y[MAXN], partner[MAXN], nxt[MAXN];

void init_nxt() {
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			if (x[i] < x[j] && y[i] == y[j])
				if (nxt[i] == 0 || x[j] - x[i] < x[nxt[i]] - x[i])
						nxt[i] = j;
}

bool check_circle() {
	/*for (int i = 1; i <= N; i++)
		if (partner[i] > i)
			cout << i << ":" << partner[i] << " ";
	cout << endl;*/
	
	for (int start = 1; start <= N; start++) {
		int pos = start;
		for (int i = 0; i < N; i++)
			pos = nxt[partner[pos]];
		
		if (pos != 0) return true;
	}
	
	return false;
}

int dfs() {
	int i;
	// To find a point that hasn't got paired
	for (i = 1; i <= N; i++)
		if (partner[i] == 0) break;
	
	// All points are paired
	if (i > N) {
		if (check_circle()) return 1;
		else return 0;
	}
	
	int total = 0;
	// Find i's partner
	for (int j = i + 1; j <= N; j++)
		if (partner[j] == 0) {
			partner[i] = j;
			partner[j] = i;
			total += dfs();
			partner[i] = partner[j] = 0;
		}
	
	return total;
}

int main() {
	freopen("wormhole.in", "r", stdin);
	freopen("wormhole.out", "w", stdout);
	
	cin >> N;
	for (int i = 1; i <= N; i++)
		cin >> x[i] >> y[i];
		
	init_nxt();
	
	cout << dfs() << endl;
	return 0;
}

 

标签:direction,01,wormhole,int,number,USACO,2021,wormholes,Bessie
来源: https://blog.csdn.net/INTEGRATOR_37/article/details/112172052