当前位置:网站首页>华为机试:连续出牌数量
华为机试:连续出牌数量
2022-07-17 12:35:00 【小朱小朱绝不服输】
【编程题目 |200分】连续出牌数量【2022 Q1,Q2考试题】
题目描述
- 手里给一副手牌,数字从0-9,有r(红色),,g(绿色),b(蓝色),y(黄色)四种颜色,出牌规则为每次打出的牌必须跟上一张的数字或者颜色相同,否则不能抽选。
- 选手应该怎么选才能使得抽选的次数最大,并且输出这个最大次数。
输入描述
- 第一行 牌的数值n (1<=n<=9)
- 第二行 牌的颜色(r,g,b,y四种颜色表示)
输出描述
- 输出最大出牌数量
示例
输入
1 4 3 4 5
r y b b r
输出
3
说明
如果打(1, r)-> (5, r),那么能打两张。
如果打(4,y) -> (4, b) -> (3, b),那么能打三张。
思路分析
这道题还可以考虑BFS,相同数字或者相同颜色的都存入连续关系。
从第一个进如队列,统计每一个对应的最大次数,最后再更新最大值。
存入连续关系得时候可以使用二维list
以序号为索引,判断元素相同或颜色相同的都加入相应的list。
然后就是常规的BFS入队进行处理。
参考代码
import java.util.*;
import java.util.Scanner;
public class lianXuChupaiNum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str1 = in.nextLine().split(" ");
String[] str2 = in.nextLine().split(" ");
String[][] arr = new String[str1.length][2];
for (int i = 0; i < str1.length; i++) {
arr[i][0] = str1[i];
arr[i][1] = str2[i];
}
// 存储相等关系
List<List<Integer>> list = new ArrayList<>(arr.length);
for (int i = 0; i < arr.length; i++) {
// 初始化
list.add(new ArrayList<>());
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i != j) {
if (arr[i][0].equals(arr[j][0]) || arr[i][1].equals(arr[j][1])) {
list.get(i).add(j);
}
}
}
}
// BFS
Queue<Integer> queue = new ArrayDeque<>();
Queue<Integer> visited = new ArrayDeque<>(); // 用于判断是否访问过
int[] res = new int[arr.length];
Arrays.fill(res, 1);
queue.add(0);
visited.add(0);
while (!queue.isEmpty()) {
int poll = queue.poll();
for (int node : list.get(poll)) {
if (!visited.contains(node)) {
visited.add(node);
res[node] = res[poll] + 1;
queue.add(node);
}
}
}
int ans = 0;
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, res[i]);
}
System.out.println(ans);
}
}
欢迎在评论区指正以及留下自己的更简洁的方法。
边栏推荐
- 如何使用SVG制作沿任意路径排布的文字效果
- 旋转矩阵(Rotate Matrix)的性质分析(转发)
- 微信小程序云开发 1 - 数据库
- 机械臂速成小指南(十三):关节空间轨迹规划
- Analysis and solution of application jar package conflict in yarn environment
- 为什么磁力变速齿轮会反转?
- 一个简单的websocket例子
- [Niuke swipe questions] / *c language realizes left-hand rotation of strings*/
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
- HCIA OSPF
猜你喜欢
随机推荐
高效理解 FreeSql WhereDynamicFilter,深入了解设计初衷[.NET ORM]
HCIA static comprehensive experiment report 7.10
中职网络安全——(2022网络安全nc批量连接的脚本)免费的脚本哦~~~
HCIA review and answer 2022.7.6
The select function of dplyr package in R language deletes the data columns in dataframe containing the specified string content (drop columns in dataframe)
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、设置na.rm参数为FALSE之后包含缺失值的分组的统计量的结果为NA
创建虚拟机第一章(vmvare虚拟机)
[Niuke swipe questions] / *c language realizes left-hand rotation of strings*/
2022年全国最新消防设施操作员(中级消防设施操作员)模拟试题及答案
Quick completion guide of manipulator (zero five): resources related to manipulator
看一看这丑恶嘴脸 | MathWorks Account Unavailable - Technical Issue
yarn(cdh)中的虚拟cpu和内存
2022年湖南省中职组“网络空间安全”赛题解析(超详细)
Studio 3T无限试用
内核态和用户态
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize dot strip plot, and set the add parameter to mean_ SD add the mean standard deviation vertical line and s
AutoJs学习-多功能宝箱-中
Convert excel table to word table, and keep the formula in Excel table unchanged
How to save and exit VIM
Analysis of the "Cyberspace Security" competition of Hunan secondary vocational group in 2022 (super detailed)









