#include <iostream>
using namespace std;
const int N=11;
int ans;
int p[N];
int e[N][N];
bool st[N];
int find(int x)
{
if(x!=p[x])p[x]=find(p[x]);
return p[x];
}
void dfs(int u)
{
if(u==8)
{
for (int i = 1; i <= 7; i ++) p[i] = i;
for (int i = 1; i <= 7; i ++)
for (int j = 1; j <= 7; j ++)
if(e[i][j] && st[i] && st[j])
p[find(i)] = find(j);
int cnt = 0;
for (int i = 1; i <= 7; i ++)
if(st[i] && p[i] == i)
cnt ++;
if(cnt == 1) ans ++;
return;
}
st[u]=1;
dfs(u+1);
st[u]=0;
dfs(u+1);
}
//a b c d e f g
//1 2 3 4 5 6 7
int main()
{
e[1][2] = e[1][6] = 1;
e[2][1] = e[2][7] = e[2][3] = 1;
e[3][2] = e[3][7] = e[3][4] = 1;
e[4][3] = e[4][5] = 1;
e[5][4] = e[5][7] = e[5][6] = 1;
e[6][1] = e[6][7] = e[6][5] = 1;
e[7][2] = e[7][3] = e[7][5] = e[7][6] = 1;
dfs(1);
cout<<ans;
return 0;
}
蓝桥杯——七段码(dfs+并查集)
最新推荐文章于 2024-07-24 22:13:06 发布