USACO 1.3 Wormholes
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 find all the possibilities.
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).
- | . . . .
- 4 3 . . . Bessie will travel to B then
- 1-2-.-.-. A then across to B again
Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 1-3 and 2-4 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).
Only the pairings 1-4 and 2-3 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling.
————————————————————————————题解
为了提醒一下自己错了那么傻【哔——】的一个错误
题目大意是一个奇怪的农夫炸出了许多黑洞【他怎么办到的……】黑洞两两联通,假如1,2黑洞是一对,从1黑洞进去会从2黑洞出来,从2黑洞进去会从1黑洞出来,然后他蠢得要死的奶牛不会躲,而且傻乎乎地往x正半轴的方向走,他的奶牛可能进入死循环,然后就gg了……然后这个炸出黑洞的人并不知道哪两个黑洞联通,我们要计算这些黑洞会在任意一点出现死循环的配对数
我们离散化一下,然后暴力搭配,然后在任意一个黑洞模拟走路就可以了,就是最后一个模拟没写好,挂了三次……
- /*
- PROB: wormhole
- LANG: C++
- ID: jiaqi si
- */
- #include <iostream>
- #include <string.h>
- #include <cstdlib>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #define ivory
- #define mo 1000000007
- #define siji(i,x,y) for(int i=(x);i<=(y);i++)
- #define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
- #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
- #define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
- #define pii pair<int,int>
- #define fi first
- #define se second
- #define mo 1000000007
- using namespace std;
- pii a[];
- int n,pline[];
- int paring[],now;
- bool use[];
- vector<int> v[];
- int ans;
- bool check(int k,int prev) {
- if(prev!=paring[k]) { return check(paring[k],k);}
- //只要它过黑洞的时候不走回去就行,之前打了标记,然而有些时候黑洞可以走两次
- else {
- int s=v[pline[k]].size();
- xiaosiji(i,,s) {
- if(a[v[pline[k]][i]].fi==a[k].fi) {
- if(i==s-) return true;
- if(v[pline[k]][i+]==now) return false;
- return check(v[pline[k]][i+],k);
- break;
- }
- }
- }
- return true;
- }
- void calculate() {
- siji(i,,n) {
- now=i;
- if(!check(i,)) {++ans;return;}
- }
- }
- void dfs(int u,int t) {
- if(t==n-) {
- siji(i,,n) if(!use[i]) {paring[u]=i;paring[i]=u;}
- calculate();return;
- }
- use[u]=;
- siji(i,,n) {
- if(!use[i]) {
- use[i]=;paring[i]=u;paring[u]=i;
- siji(j,,n) {
- if(!use[j]) {dfs(j,t+);break;}
- }
- use[i]=;
- }
- }
- use[u]=;
- }
- int main()
- {
- #ifdef ivory
- freopen("wormhole.in","r",stdin);
- freopen("wormhole.out","w",stdout);
- #else
- freopen("f1.in","r",stdin);
- #endif
- scanf("%d",&n);
- siji(i,,n) {
- scanf("%d%d",&a[i].fi,&a[i].se);
- swap(a[i].fi,a[i].se);
- }
- sort(a+,a+n+);
- int it=a[].fi,cnt=;
- siji(i,,n) {
- if(a[i].fi==it) {v[cnt].push_back(i);pline[i]=cnt;}
- else {it=a[i].fi;++cnt;v[cnt].push_back(i);pline[i]=cnt;}
- swap(a[i].fi,a[i].se);
- }
- dfs(,);
- printf("%d\n",ans);
- return ;
- }
USACO 1.3 Wormholes的更多相关文章
- [题解]USACO 1.3 Wormholes
Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...
- USACO 1.3 Wormholes - 搜索
Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...
- USACO Section1.3 Wormholes 解题报告
wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...
- USACO Wormholes 【DFS】
描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他的虫洞将形成 N/2 ...
- USACO Section 1.3 Wormholes 解题报告
题目 题目描述 在一个二维平面上有N个点,这N个点是(N/2)个虫洞的端点,虫洞的特点就是,你以什么状态从某个端点进去,就一定会以什么状态从另一端的端点出来.现在有一头牛总是沿着与X轴正方向平行的直线 ...
- 【USACO 1.3】Wormholes
/* LANG: C++ TASK: wormhole n个洞,n<=12, 如果两洞配对,则它们之间有地下路径(无向) 牛在地上只会往+x方向 问多少种两两配对的方案,牛从地上某位置出发,会陷 ...
- 「日常训练」「小专题·USACO」 Wormholes(1-4)
题意 之后补充. 分析 这是一条很好的考察递归(或者说搜索)的题目.它的两个过程(建立初步解,验证)都用到了递归(或者说运用递归可以相当程度的减少代码量). 具体实现见代码.注意,为了使用std::p ...
- 【USACO】wormholes 【暴力】
题意:给出2K个平面上的点,给它们一一配对,问有多少种配对方法使得存在从某个点一直向右走会陷在循环里(K<=6) 思路:由于k很小,配对方法的话暴力枚举,然后判环,判环时需要注意的是一条直线上的 ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
随机推荐
- Python3.5环境下安装wxPtyhon
Win7系统下,Python3.5环境下安装wxPtyhon, 已成功安装并运行. 1.先从下面网站下载对应的whl版本. https://wxpython.org/Phoenix/snapshot- ...
- [pinyin4j] java版汉字转换拼音(大小写)
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...
- Java动态代理简单应用
概念 代理模式是基本的设计模式之一,它是开发者为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. Java动态代理比 ...
- 【原生js】js动态添加dom,如何绑定事件
首先要明白浏览器在加载页面的时候是按顺序来加载的,这样以来就很清楚了,js动态添加dom以后,这些dom并没有绑定事件,这个时候最简单的一个办法就是:将绑定事件的方法封装到一个函数A中,在动态添加完d ...
- C语言指针、地址、赋值三者含义
先来一个观点.大家先看看对不对 按:在CSDN论坛上,有位坛友提到这个问题: ==================================== 先看一段代码: #include<stdi ...
- Ubuntu 14.04—无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 解决办法
在Ubuntu中使用sudo apt-get install安装是有时候会出现: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 解决办法 这样的错误,这是因为更新源 ...
- css学习之 display:inline-block;
设置display:inline-block;后的元素 就是一个格式化为行内元素的块容器( Block container ):通俗讲就是:将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内 ...
- Kafka单机版安装(CentOS 7环境下)
一.环境操作系统和软件版本介绍 1.环境操作系统为CentOS Linux release 7.2.1511 (Core) 可用cat /etc/redhat-release查询 2.软件版本 Kaf ...
- 新Mac 开机启动MySQL/MongoDB/Redis 等服务
在Mac上我们使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)来安装和管理开发工具包,例如:mysql.php.redis.只需要一个命令 brew ...
- 绿色版的mysql安装配置方式
解压下载好的压缩包 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的原版文件为 # For advice on how to ...