[题解]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.
Submission file Name: USACO Gateway | Comment or Question
(转自[USACO])
讲一下题目大意。Farmer John(USACO的标志人物啊)特别喜欢做实验,使得农场上出现了N(N <= 12)个虫洞,现在将这N个虫洞两两配对,配对了的虫洞从其中一个进入就会从对应的一个虫洞出来。Bessie在田园中总是向x轴的正方向前进。John想知道,有多少种虫洞的配对方案可以让Bessie在田园中某一个地方出发,会陷入死循环。
很明显的搜索(数据范围小),因为(1,2)(3,4)和(3,4)(1,2)是同一种匹配,所以每一次搜索的时候就找到编号最小的一个虫洞和其他的虫洞匹配。
接下来就是判断是否会陷入死循环。还算比较好想。首先给每个虫洞预处理出一个right,表示从它出来,往右走遇到的第一个虫洞的编号,如果不存在,就记个0吧。然后用个数组记录一下类似于Tarjan的时间戳的一个东西,每次循环访问的时候记一个编号(当然设成一样的),如果下一虫洞没有访问,那么就把编号设成当前的编号,否则判断它是否和现在的编号相等,如果相等,就说明会陷入死循环。
Code
/*
ID:
PROG: wormhole
LANG: C++11
*/
/**
* USACO
* Accpeted
* Time:0ms
* Memory:4184k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} typedef class Point {
public:
int x;
int y;
int id;
boolean operator < (Point another) const {
if(this->y != another.y) return this->y < another.y;
return this->x < another.x;
}
}Point; int n;
int* onright;
Point* ps; inline void init(){
readInteger(n);
ps = new Point[(const int)(n + )];
onright = new int[(const int)(n + )];
for(int i = ; i <= n; i++){
readInteger(ps[i].x);
readInteger(ps[i].y);
ps[i].id = i;
onright[i] = ;
}
} int* matches;
boolean* seced;
int res; boolean check(){
int vis[(const int)(n + )];
memset(vis, , sizeof(vis));
vis[] = -;
for(int i = ; i <= n; i++){
if(vis[i] != ) continue;
int p = i;
while(vis[p] == ){
vis[p] = i;
p = onright[p];
if(seced[p]) p = matches[p];
}
if(vis[p] == i) return true;
}
return false;
} void search(int choosed){
if(choosed + > n){
if(check()) res++;
return;
}
int sta;
for(int i = ; i <= n; i++)
if(!seced[i]){
sta = i;
break;
}
seced[sta] = true;
for(int i = sta + ; i <= n; i++){
if(!seced[i]){
matches[sta] = i;
matches[i] = sta;
seced[i] = true;
search(choosed + );
seced[i] = false;
}
}
seced[sta] = false;
} inline void solve(){
matches = new int[(const int)(n + )];
seced = new boolean[(const int)(n + )];
memset(seced, false, sizeof(boolean) * (n + ));
sort(ps + , ps + n + );
for(int i = ; i < n; i++){
if(ps[i + ].y == ps[i].y){
onright[ps[i].id] = ps[i + ].id;
}
}
search();
printf("%d\n", res);
} int main(){
freopen("wormhole.in", "r", stdin);
freopen("wormhole.out", "w", stdout);
init();
solve();
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 1.3 Ski Course Design
Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...
- USACO Section1.3 Wormholes 解题报告
wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...
- 题解 [USACO Mar08] 奶牛跑步
[USACO Mar08] 奶牛跑步 Description Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘,然后走回牛棚. Bessie也不想跑得太远,所 ...
- [题解]USACO 5.2.1 Snail Trails
链接:http://cerberus.delos.com:791/usacoprob2?S=snail&a=uzElkgTaI9d 描述:有障碍的棋盘上的搜索,求从左上角出发最多经过多少个格子 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
随机推荐
- JAVA 多线程和并发学习笔记(四)
1. 多进程 实现并发最直接的方式是在操作系统级别使用进程,进程是运行在它自己的地址空间内的自包容的程序.多任务操作系统可以通过周期性地将CPU从一个进程切换到另一个进程,来实现同时运行多个进程. 尽 ...
- JS定义函数的两种方式:函数声明和函数表达式
函数声明 关于函数声明的方式,它的一个重要的特性就是函数声明提升(function declaration hoisting),意思是在执行代码之前会先读取函数声明.这就意味着可以把函数声明放在调用它 ...
- Log.properties配置详解
一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...
- 关于Hibernate XXX is not mapped 错误
我的实体类是这么配置的 @Entity(name="EntityName") //必须,name为可选,对应数据库中一的个表 就会出现 XXX is not mapped. ...
- 动态规划小结(dynamic programming)
动态规划在很多情况下可以降低代码的空间时间复杂度. 判断一道问题能否应用动态规划,需要关注问题是否具有最优子结构,当前规模的问题的解在之前问题的解里面,还要注意的是要满足无后效性的原则.随后就是寻找递 ...
- [转载]Python 资源大全
原文链接:Python 资源大全 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex ...
- OpenGL ES 中的模板测试
模板测试的主要功能是丢弃一部分片元,相对于深度检测来说,模板测试提出的片元数量相对较少.模板测试发生在剪裁测试之后,深度测试之前. 使用模板测试时很重要的代码提示: 1.glClear( GL_STE ...
- 关于GPL的一些知识
1.什么是GPL GPL许可协议(GNU General Public License):只要软件中包含有其他GPL协议的产品或代码,那么该软件就必须也采用GPL许可协议且开源及免费.具有以下特点: ...
- Broadcom以太网交换芯片培训
目录 1.交换芯片架构....................................................................................... ...
- C语言typedef的用法(转)
http://www.cnblogs.com/afarmer/archive/2011/05/05/2038201.html 一.基本概念剖析 int* (*a[5])(int, char*); ...