题意:有12张连在一起的12生肖的邮票。现在你要从中剪下5张来,要求必须是连着的。(仅仅连接一个角不算相连)
分析:暴力+并查集。
1、记录下每个数字所在位置。
2、先枚举各不相同的5个数的所有可能情况(不包括数字种类相同但次序不同的情况)。
2、然后判断若其中某两个数字相邻则加入一个连通块,如果最终只有一个连通块,说明5个数字是通过相邻关系连在一起的,符合要求。
#include "stdafx.h"
#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]];//将每个数的横坐标放入tmpx;
int tmpy = stay[a[i]];//将每个数的纵坐标放入tmpy;
for(int j = 0; j < 5; ++j){
if(i == j) continue;//跳过相同。
int tx = stax[a[j]];//同上tmpx;
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));//将每个数的祖先都插入set当中,利用set去重,也就是确定祖先是否唯一;
}
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;
}//stax存储坐标;直接用一位数组表示二维坐标,
}
ans = 0;
dfs(0, 0);
printf("%d\n", ans);
return 0;
}
转载自https://www.cnblogs.com/tyty-Somnuspoppy/p/6680082.html