题目:剪邮票
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
解题思路:枚举五个点, 通过dfs或bfs或并查集检查这五个点是否是一个连通块
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int arr[3][4], cnt, dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void set(int a, int b, int c, int d, int e, int num){
arr[(a-1)/4][(a-1)%4]=num;
arr[(b-1)/4][(b-1)%4]=num;
arr[(c-1)/4][(c-1)%4]=num;
arr[(d-1)/4][(d-1)%4]=num;
arr[(e-1)/4][(e-1)%4]=num;
}
void dfs(int x, int y){
arr[x][y] = 0;
for(int i=0; i<4; ++i){
int nowx = x+dir[i][0], nowy = y+dir[i][1];
if(nowx>=0 && nowx<=2 && nowy>=0 && nowy<=3 && arr[nowx][nowy]){
cnt++;
dfs(nowx, nowy);
}
}
}
int main()
{
memset(arr, 0, sizeof(arr));
int ans = 0;
for(int a=1; a<=8; ++a){
for(int b=a+1; b<=9; ++b){
for(int c=b+1; c<=10; ++c){
for(int d=c+1; d<=11; ++d){
for(int e=d+1; e<=12; ++e){
set(a, b, c, d, e, 1);
cnt = 1;
dfs((a-1)/4, (a-1)%4);
if(cnt == 5){
ans++;
}
set(a, b, c, d, e, 0);
}
}
}
}
}
printf("%d", ans);
return 0;
}