第七题:
剪邮票
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
答案:116 暴力吧。注意序列要递增/递减。这样就不会出现重复了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long int LL;
#define INF 0x3f3f3f3f
int ans = 0;
int num[15],n,g[15][15],vis[15][15],tot;
int dist[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
void dfs(int x, int y)
{//判选择的5个数组的坐标是否是连通的。
for (int i = 0; i < 4; i++)
{
int nextx = x + dist[i][0];
int nexty = y + dist[i][1];
if ((nextx >= 0 && nextx < 3 && nexty >= 0 && nexty < 4) && !vis[nextx][nexty]&&g[nextx][nexty])
{
vis[nextx][nexty] = 1;
tot++;
dfs(nextx, nexty);
}
}
}
bool check()
{//判断当前组合是否合法。
tot = 0;
memset(vis, 0, sizeof(vis));
memset(g, 0, sizeof(g));
int x, y;
for (int k = 0; k < 5; k++)
{//把5个数组的位置转换成对于坐标并标记
x = num[k] / 4;
y = num[k] % 4;
g[x][y] = 1;
}
tot++;
vis[x][y] = 1;
dfs(x, y);
if (tot == 5)//如果连通量=5,即剪出的邮票是连续的
{
return true;
}
else
{
return false;
}
}
int main()
{
for(int a1=0;a1<12-4;a1++)
{
for(int a2=a1+1;a2<12-3;a2++)
{
for(int a3=a2+1;a3<12-2;a3++)
{
for(int a4=a3+1;a4<12-1;a4++)
{
for(int a5=a4+1;a5<12;a5++)
{
num[0]=a1; num[1]=a2; num[2]=a3;
num[3]=a4; num[4]=a5;
if(check())
{
ans++;
}
}
}
}
}
}
printf("%d\n", ans);
return 0;
}