[蓝桥杯2020] Question E. Seven-segment code

The Second Provincial Tournament of the 11th Blue Bridge Cup-Fill in the Blank Question E

Title description:

Xiaolan uses a seven-segment digital tube to represent a special kind of text.
The figure above shows an illustration of a seven-segment digital tube. There are a total of 7-segment diodes in the digital tube that can emit light
, marked as a, b, c, d, e, f, g.
Xiaolan chooses a part of the diode (at least one) to emit light to express characters. When designing
the expression of characters , all light-emitting diodes are required to be connected in one piece.
For example: b is illuminated, other diodes do not emit light can be used to express a character.
For example: c light-emitting, other diodes not light-emitting can be used to express a character. This scheme and
the scheme on the previous line can be used to represent different characters, although they look similar.
For example: a, b, c, d, e glow, f, g not glow can be used to express a character.
For example: b, f are luminous, and other diodes cannot be used to express a character if they are not because the luminous
diodes are not connected together.
Excuse me, how many different characters can Xiaolan express with a seven-segment digital tube?

Answer: 80

Idea: dfs searches all states and judges whether each state is feasible. The method of judgment is to treat each lamp tube as a node, connect the edges to build a graph, and check each time to determine whether the currently selected lamp tube is in the same set.

program:

#include<bits/stdc++.h>
using namespace std;
const int N=10;
int use[N],ans,e[N][N],fa[N];
void init(){
    
    
	//连边建图
	//a b c d e f g
	//1 2 3 4 5 6 7
	e[1][2]=e[1][6]=1;
	e[2][1]=e[2][7]=e[2][3]=1;
	e[3][2]=e[3][4]=e[3][7]=1;
	e[4][3]=e[4][5]=1;
	e[5][4]=e[5][6]=e[5][7]=1;
	e[6][1]=e[6][5]=e[6][7]=1;
}
int find(int u){
    
    if(fa[u]==u)return u;fa[u]=find(fa[u]);return fa[u];}//并查集
void dfs(int d){
    
    
	if(d>7){
    
    
		/* 并查集判联通 */
		for(int i=1;i<=7;i++)fa[i]=i;
		for(int i=1;i<=7;i++)
		for(int j=1;j<=7;j++)
			if(e[i][j]&&use[i]&&use[j]){
    
    
				int fx=find(i),fy=find(j);
				if(fx!=fy){
    
    
					fa[fx]=fy;
				}
			}
		int k=0;
		for(int i=1;i<=7;i++)if(use[i]&&fa[i]==i)k++;
		if(k==1)ans++;
		return;
	}
	use[d]=1;
	dfs(d+1);
	use[d]=0;
	dfs(d+1);
}
int main(){
    
    
	init();
	dfs(1);
	cout<<ans;
}

Postscript: In order to save trouble on the field, the map was built, but I didn’t write and check the Unicom. I only used violence to determine whether the side is connected, but I forgot that there may be two Unicom blocks not connected. TvT was lost for nothing. 15 points

Guess you like

Origin blog.csdn.net/qq_45530271/article/details/109189978