题意:有12张连在一起的12生肖的邮票。现在你要从中剪下5张来,要求必须是连着的。(仅仅连接一个角不算相连)

分析:暴力+并查集。

1、记录下每个数字所在位置。

2、先枚举各不相同的5个数的所有可能情况(不包括数字种类相同但次序不同的情况)。

2、然后判断若其中某两个数字相邻则加入一个连通块,如果最终只有一个连通块,说明5个数字是通过相邻关系连在一起的,符合要求。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 5000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int stax[20];
int stay[20];
int fa[20];
int a[10];
int ans;
int Find(int x){
return fa[x] = (fa[x] == x) ? x : Find(fa[x]);
}
bool judge(){
for(int i = 0; i < 20; ++i) fa[i] = i;
for(int i = 0; i < 5; ++i){
int tmpx = stax[a[i]];
int tmpy = stay[a[i]];
for(int j = 0; j < 5; ++j){
if(i == j) continue;
int tx = stax[a[j]];
int ty = stay[a[j]];
int x = abs(tmpx - tx);
int y = abs(tmpy - ty);
if((x == 0 && y == 1) || (x == 1 && y == 0)){
int ii = Find(i);
int jj = Find(j);
if(ii == jj) continue;
if(ii < jj) fa[jj] = ii;
else fa[ii] = jj;
}
}
}
set<int> s;
for(int i = 0; i < 5; ++i){
s.insert(Find(i));
}
return s.size() == 1;
}
void dfs(int cur, int st){
if(cur == 5){
if(judge()){
++ans;
}
return;
}
for(int i = st + 1; i <= 12; ++i){
a[cur] = i;
dfs(cur + 1, i);
}
}
int main(){
int cnt = 0;
for(int i = 1; i <= 3; ++i){
for(int j = 1; j <= 4; ++j){
++cnt;
stax[cnt] = i;
stay[cnt] = j;
}
}
ans = 0;
dfs(0, 0);
printf("%d\n", ans);
return 0;
}

  

最新文章

  1. asp:Repeater实例备忘
  2. js获取项目根目录的方法
  3. 由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求。错误: 0x80131902
  4. composer 272解决
  5. jQuery细节总结
  6. 华为的JAVA面试题及答案(部分)
  7. oracle创建job方法
  8. bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
  9. c#中使用ABCpdf处理PDF
  10. 在Android模拟器中经常出现以下错误 timeout Launch canceled!
  11. ul中li分列显示
  12. 为什么32位操作系统最大支持4GB内存
  13. HTML 5 服务器发送事件、Input 类型、表单元素、表单属性
  14. USACO Section 1.1-3 Friday the Thirteenth
  15. HDU-AcmKeHaoWanLe训练实录
  16. Generator
  17. 学习笔记DL001:数学符号、深度学习的概念
  18. Python3.7版本unittest框架添加用例的方法
  19. html + js 实现图片上传,压缩,预览及图片压缩后得到Blob对象继续上传问题
  20. Python-查找两个文件中相同的ip地址

热门文章

  1. Hadoop操作经验
  2. Linux 创建网卡子接口
  3. java中将图片上传到配置好的ftp服务器上
  4. luogu P2756 飞行员配对方案问题(Dinic板子)
  5. MySQL日常使用笔记
  6. MQTT 协议学习:000-有关概念入门
  7. Windows驱动开发-设备读写方式
  8. Why Helm?【转】
  9. define可变参数,float数据传输
  10. 九宫格 android:stretchMode=&quot;columnWidth&quot;,缩放与列宽大小同步