当前位置:网站首页>华为机试:连续出牌数量
华为机试:连续出牌数量
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);
}
}
欢迎在评论区指正以及留下自己的更简洁的方法。
边栏推荐
- HCIA rip experiment 7.11
- ty_ Gr551x code framework
- NAT技术及NAT ALG
- 日期——贵州大学考研机试题目
- R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用subset函数筛选满足条件的数据子集构建回归模型)
- Effectively understand FreeSQL wheredynamicfilter and deeply understand the original design intention [.net orm]
- 无法改变的现状
- C language structure to realize simple address book
- Studio 3T unlimited trial
- 华为无线设备配置智能漫游
猜你喜欢

华为防火墙认证技术

微信小程序云开发 1 - 数据库

The use and Simulation of stack and queue in STL

潇洒郎:VMware固定虚拟机IP地址

如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例

HCIA RIP实验 7.11

024.static与final使用陷阱 续

Virtual CPU and memory in yarn (CDH)

Three programming implementations to quickly determine whether the site is alive

Convert excel table to word table, and keep the formula in Excel table unchanged
随机推荐
FreeRTOS个人笔记-临界值的保护
2022 Hunan secondary vocational group "Cyberspace Security" packet analysis infiltration Pacpng parsing (super detailed)
Attachment handling of SAP Fiori
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
Analysis of Web Remote Code Execution Vulnerability of Zhongke panyun-d module
String类型函数传递问题
Job: enter an odd number of 1-100
中职网络安全——(2022网络安全nc批量连接的脚本)免费的脚本哦~~~
选择比努力更重要
基于JSP的小说写作与创作网站
Lvi-sam: laser IMU camera tight coupling mapping
机械臂速成小指南(十三):关节空间轨迹规划
架构实战营|模块7
Stream流
String type function transfer problem
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语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化分组直方图、使用palette参数自定义分组直方图的条形边框颜色
Microsoft OneNote tutorial, how to insert mathematical formulas in OneNote?
Distinction between private key and public key -- Explanation of private key and public key
Smart Lang: VMware fixed virtual machine IP address