当前位置:网站首页>蓝桥杯冲刺——二进制枚举
蓝桥杯冲刺——二进制枚举
2022-04-22 05:38:00 【柴可拉夫斯基】
什么是二进制枚举?
链接: 二进制枚举.
七段码
链接: 七段码.

这一题有点绕弯子了,代码修改了好一会
这里的判断条件稍微多一些。
整体思路就是:
- 枚举所有情况
- 对每种情况通过并查集判断是否连通
枚举的话我这里使用的是二进制枚举,优势就是比较快,不过这里既是填空题,总共又只有120来种情况,用DFS也是可以的
判断连通的话主要分为并和判断两方面
- 并:只要两个灯相连就合并。
在这里我考虑多了,由于我们是从前往后,且不重复判断的,不用考虑断掉或者是由于它有两端,头不一致的情况。
- 判断:我们把并查集整理好以后,只要pre[t]还是自己(t)的情况就是连在一起的一段(可以画图理解一下)。也就是说,段数有大于1,就不是连通的
完整代码
import java.util.Scanner;
public class test {
public static int count=0;
public static boolean[][] l = new boolean[8][8];
public static void init() {
l[1][1] = l[1][2]=l[1][6] = true;
l[2][2] = l[2][1]=l[2][7] = l[2][3]=true;
l[3][3] = l[3][2]=l[3][4] = l[3][7]=true;
l[4][4] = l[4][3]=l[4][5] = true;
l[5][5] = l[5][4]=l[5][7] = l[5][6]=true;
l[6][6] = l[6][1]=l[6][7] = l[6][5]=true;
l[7][7] = l[7][2]=l[7][3] = l[7][5]=l[7][6]=true;
}
public static void main(String[] args) {
init();
for(int i=0;i<(1<<7);i++) {
int[] light = new int[8];
for(int j=0;j<7;j++) {
if((i&(1<<j))>0) {
light[j+1]=1;
}
}
System.out.println();
if(check(light)) {
count++;
}
}
System.out.println(count);
}
public static boolean check(int[] lights) {
int i=1,j=1;
int[] pre= {
0,1,2,3,4,5,6,7};
while(i<8) {
if(lights[i]==1) {
for(j=i+1;j<8;j++) {
if(lights[j]==1) {
if(l[i][j]) {
join(i,j,pre);
}
}
}
}
i++;
}
int count=0;
for(int t=1;t<8;t++) {
if(lights[t]==1&&pre[t]==t)
count++;
}
return count==1;
}
public static int find(int x,int[] pre) {
if(pre[x] == x) return x;
return pre[x] = find(pre[x],pre);
}
public static void join(int x,int y,int[] pre) {
int px = find(x,pre),py = find(y,pre);
if(px!=py)
pre[px] = py;
}
}
版权声明
本文为[柴可拉夫斯基]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53421929/article/details/123998110
边栏推荐
- Random string tool class randomstringutils detailed explanation
- miniconda 换源(添加镜像)
- mysql建表最佳实践
- List分割最佳实践
- Why introduce collaborative process
- 数的范围( 二分 经典模板题目)
- Simple DP questions - cow breeding and super stair climbing
- acwing854. Floyd求最短路
- Missile interception problem (DP, Dilworth theorem)
- LeetCode 486. 预测赢家--动态规划+博弈论
猜你喜欢

Data mining -- sequential pattern mining

数据挖掘——序列模式挖掘

《最优化理论》:运输问题(一)求最小运费【西北角法、最小元素法、伏格尔法】

数据挖掘——数据预处理

The breakpoint will not currently be hit No symbols have been loaded for this document.

Digital DP (template)

卷积神经网络

LeetCode 1591. 奇怪的打印机 II --判断排序

广度优先搜索专题(bfs)

Force buckle 876 Intermediate node of linked list
随机推荐
Single machine deployment redis master-slave and sentinel mode (one master, two slave and three sentinels)
Why introduce collaborative process
关于form表单点击submit按钮后,页面自动刷新的问题解决
TCGA数据库ensembl id 转为 gene Symbol,提取出需要的RNA种类表达谱列表信息
MySQL basic commands and exercises (I)
imdecode 源码解读
The difference between set method and add method in list
折现分割平面
树莓派4B ssh connection refused
机器学习——用鸢尾花数据集画P-R曲线和ROC曲线
RPC必知必会
Code analysis of STL alloc space allocator
Digital triangle (dynamic programming DP)
Installing mysql8 under Linux
SQL面试题总结(更新中)
torch使用踩坑日记,矩阵加速运算
typescript:基础知识
Force buckle 876 Intermediate node of linked list
记录一次项目经历和项目中遇到的技术
LeetCode 面试题 17.09. 第 k 个数--动态规划