2016年蓝桥杯预赛第七题剪邮票

题目:剪邮票
如【图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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值