华为OD机试真题 - 连续出牌数量
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
String[] c = in.nextLine().split(" ");
List cards = new ArrayList();
for (int i = 0; i < nums.length; i++) {
cards.add(new Card(nums[i], c[i].charAt(0)));
}
System.out.println(getResult(cards));
}
public static int getResult(List cards) {
Queue path = new PriorityQueue<>(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < cards.size(); i++) {
List temp = cards.stream().collect(Collectors.toList());
temp.remove(i);
DFS(temp, path,cards.get(i), 1);
}
if (path.size() > 0)
return path.poll();
return 0;
}
public static void DFS(List totol, Queue path, Card cur, int depth) {
boolean has = false;
for (int i = 0; i < totol.size(); i++) {
Card temp = totol.get(i);
if (temp.num == cur.num || temp.color == cur.color) {
List copy = totol.stream().collect(Collectors.toList());
has = true;
copy.remove(i);
DFS(copy, path, totol.get(i), depth + 1);
}
}
if (!has) {
path.add(depth);
}
}
static class Card {
public int num;
public char color;
public Card(int num, char color) {
this.num = num;
this.color = color;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Card)
return false;
Card c = (Card) obj;
if (this.num == c.num && this.color == c.color)
return true;
return false;
}
}
Scanner in = new Scanner(System.in);
int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
String[] c = in.nextLine().split(" ");
List cards = new ArrayList();
for (int i = 0; i < nums.length; i++) {
cards.add(new Card(nums[i], c[i].charAt(0)));
}
System.out.println(getResult(cards));
}
public static int getResult(List
Queue
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < cards.size(); i++) {
List
temp.remove(i);
DFS(temp, path,cards.get(i), 1);
}
if (path.size() > 0)
return path.poll();
return 0;
}
public static void DFS(List
boolean has = false;
for (int i = 0; i < totol.size(); i++) {
Card temp = totol.get(i);
if (temp.num == cur.num || temp.color == cur.color) {
List
has = true;
copy.remove(i);
DFS(copy, path, totol.get(i), depth + 1);
}
}
if (!has) {
path.add(depth);
}
}
static class Card {
public int num;
public char color;
public Card(int num, char color) {
this.num = num;
this.color = color;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Card)
return false;
Card c = (Card) obj;
if (this.num == c.num && this.color == c.color)
return true;
return false;
}
}
全部评论
相关推荐
10-31 11:57
门头沟学院 Java 点赞 评论 收藏
分享