usaco.section1.3 wormhole(枚举)
来源:互联网 发布:mac系统打不开exe文件 编辑:程序博客网 时间:2024/05/07 07:31
有 N 个虫洞,将这些虫洞两两分组,同组的两个虫洞可以互相到达,使得奶牛从坐标系某个位置出发,一直沿+x 方向走时会陷入无限循环
因为 N <= 12 比较小,可以枚举所有的两两连接情况,然后判断可行不可行
#include <cstdio>using namespace std;const int MAX_N=12;int N, x[MAX_N + 1], y[MAX_N + 1];int next[MAX_N + 1], p[MAX_N + 1];bool check(void){for (int i=1; i<=N; i++){int pos = i; // every startfor (int j=1; j<=N; j++) pos = next[p[pos]];if(pos != 0) return true;}return false;}int doit(void){// counting all possible casesint i, ret=0;for (i=1; i<=N; i++) if(p[i] == 0) { i=i; break; } // finding the wormhole haven't be paired if (i>N){ // there is not unpaired wormholeif(check())return 1;return 0;}for (int j=i+1; j<=N; j++) // pairing i with j if(p[j] == 0){ p[j] = i; p[i] = j; ret += doit(); p[j] = p[i] = 0; }return ret;}int main(void){freopen("wormhole.in", "r", stdin);freopen("wormhole.out", "w", stdout);scanf("%d", &N);for (int i=1; i<=N; i++) scanf("%d%d", &x[i], &y[i]); // cinfor (int i=1; i<=N; i++)// finding next[] for (int j=1; j<=N; j++){ if (x[i] < x[j] && y[i] == y[j]){ if (next[i] == 0 || x[j] - x[i] < x[next[i]] - x[i]) // haven't got or closernext[i] = j;} }printf("%d\n", doit());return 0;}
0 0
- usaco.section1.3 wormhole(枚举)
- wormhole-section1.3
- USACO-Section1.3 Prime Cryptarithm【暴力枚举】
- USACO-Section1.3 Combination Lock【暴力枚举】
- USACO-section1.3 Combination Lock[枚举]
- USACO-Section1.2 transform[枚举]
- USACO-Section1.4 airprog[枚举]
- USACO-Section1.3 Wormholes 【深度优先搜索】【暴力枚举】
- USACO-Section1.3 Ski Course Design【暴力枚举】
- USACO wormhole
- usaco wormhole
- USACO-Section1.3 Wormholes
- USACO-Section1.3 Wormholes
- usaco.section1.4.Arithmetic Progressions(枚举)
- USACO-Section1.4 Arithmetic Progressions【暴力枚举】
- USACO-Section1.4 Arithmetic Progressions [枚举]
- USACO-Section1.5 Prime Palindromes【暴力枚举】
- USACO Section1.3 混合牛奶
- 用抓包软件来分析tcp 数据包
- ExcellenceMinimum Operational Standards
- android应用名称设置的问题
- unison实现文件双向同步
- iOS开发笔记之四十二——一个double精度导致的bug
- usaco.section1.3 wormhole(枚举)
- oracle处理乱码的问题
- Android--获取标题栏,状态栏,屏幕高度
- wxPython笔记(Getting started with wxPython 3)
- 内存分析
- 微信扫码支付-原生支付-流程图
- opencv-压缩图片
- 数据挖掘算法基础-关联规则
- Android Service和Activity通信