蓝桥杯 剪邮票
作者:互联网
传送门
#include <iostream>
#include <algorithm>
using namespace std;
int a[12] = { 0,0,0,0,0,0,0,1,1,1,1,1 };//枚举剪的五张邮票
int ans = 0;
void dfs(int g[3][4], int i, int j) {
g[i][j] = 0;
if (i - 1 >= 0 && g[i - 1][j] == 1)dfs(g, i - 1, j);
if (i + 1 <= 2 && g[i + 1][j] == 1)dfs(g, i + 1, j);
if (j - 1 >= 0 && g[i][j - 1] == 1)dfs(g, i, j - 1);
if (j + 1 <= 3 && g[i][j + 1] == 1)dfs(g, i, j + 1);
}//dfs连通性检查
bool check() {
int g[3][4] = { 0 };
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
if (a[i * 4 + j] == 1)//二维数组转一维数组
g[i][j] = 1;
else
g[i][j] = 0;
int cnt = 0;//连通块的个数
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
if (g[i][j] == 1) {
dfs(g, i, j);
cnt++;
}
return cnt == 1;
}
int main() {
do {
if (check())
ans++;
} while (next_permutation(a, a + 12));//全排列
cout << ans;
return 0;
}
标签:邮票,int,dfs,蓝桥,&&,include 来源: https://blog.csdn.net/weixin_45333771/article/details/104815496