A25250. 编程实现:Adam 和 Bob 在玩一种卡片接龙的游戏,他们各自有 n 张卡片,每张卡片写了一个号码,桌上还有一张写了号码 m 的卡片,他们会尽可能将手中的卡片进行接龙,游戏规则如下:(1)由 Adam 开始,双方轮流接龙;(2)接龙一方每次可以放置一张卡片到桌上,这张卡片必须比桌上的最小号码小 1,或者比桌上的最大号码大1,重复这个过程直到没有可以放置的卡片,然后轮到另一方接龙;(3)胜负判定…
题目描述
编程实现:
Adam 和 Bob 在玩一种卡片接龙的游戏,他们各自有 n 张卡片,每张卡片写了一个号码,桌上还有一张写了号码 m 的卡片,他们会尽可能将手中的卡片进行接龙,游戏规则如下:
(1)由 Adam 开始,双方轮流接龙;
(2)接龙一方每次可以放置一张卡片到桌上,这张卡片必须比桌上的最小号码小 1,或者比桌上的最大号码大1,重复这个过程直到没有可以放置的卡片,然后轮到另一方接龙;
(3)胜负判定:
(3.1)如果一方率先将手中的卡片全部放置(手中的卡片数量为 0),则这一方获得胜利;
(3.2)如果双方都还有卡片,且都不能再放置卡片,则手中卡片较少的一方获得胜利;如果卡片数量相等,则平局。
给定 Adam 和 Bob 各自的卡片数量 n,以及双方的卡片号码,桌上的卡片号码 m。请计算出接龙游戏胜利方的名字和他手中剩余卡片的数量,如果平局,则输出 Draw 和 -1。
例如:n = 4;Adam 手中 4 张卡片的号码是 5、7、9、2;Bob 手中 4 张卡片的号码是 3、6、10、4;
m = 8,桌上卡片的号码是 8,初始由 Adam 开始,接龙情况如下:
(1)Adam 手中有比桌上最小号码 8 小 1 的卡片 7,将卡片 7 放置到桌上,手中还有比桌上最大号码 8 大 1的卡片 9,将卡片 9 放置到桌上,此时手中还有卡片 5、2,没有符合接龙的卡片,轮到 Bob;

(2)Bob 手中有比桌上最小号码 7 小 1 的卡片 6,将卡片 6 放置到桌上,手中还有比桌上最大号码 9 大 1的卡片 10,将卡片 10 放置到桌上,此时手中还有卡片 3、4,没有符合接龙的卡片,轮到 Adam;

(3)Adam 手中有比桌上最小号码 6 小 1 的卡片 5,将卡片 5 放置到桌上,此时手中还有卡片 2,没有符合接龙的卡片,轮到 Bob;

(4)Bob 手中有比桌上最小号码 5 小 1 的卡片 4,将卡片 4 放置到桌上,手中还有比桌上最小号码 4 小 1的卡片 3,将卡片 3 放置到桌上,此时手中卡片数量为 0,Bob 胜利。

输出 Bob 和 0。
输入描述:
第一行输入一个整数 n(2≤n≤1000),表示 Adam 和 Bob 的卡片数量;
第二行输入 n 个整数(1≤整数≤2000),表示 Adam 手中卡片的号码,整数之间以一个空格隔开;
第三行输入 n 个整数(1≤整数≤2000),表示 Bob 手中卡片的号码,整数之间以一个空格隔开;
第四行输入一个整数 m(1≤m≤2000),表示初始桌上的卡片号码。
输出描述:
第一行输出一个字符串,表示胜利方的名字,如果平局,则输出 Draw;
第二行输出一个整数,表示胜利方手中剩余卡片的数量,如果平局,则输出 -1。
样例输入:
4
5 7 9 2
3 6 10 4
8样例输出:
Bob
0参考答案
def main():
n = int(input().strip())
adam_list = list(map(int, input().split()))
bob_list = list(map(int, input().split()))
m = int(input().strip())
# Initialize dictionaries and counts for Adam and Bob
adam_dict = {}
for a in adam_list:
adam_dict[a] = adam_dict.get(a, 0) + 1
count_adam = n
bob_dict = {}
for b in bob_list:
bob_dict[b] = bob_dict.get(b, 0) + 1
count_bob = n
# Initialize table state
left = m
right = m
turn = 0 # 0: Adam's turn, 1: Bob's turn
last_pass = False # Track if the last turn passed without playing
while True:
# Select current player
if turn == 0:
player_dict = adam_dict
player_count = count_adam
else:
player_dict = bob_dict
player_count = count_bob
has_played = False
changed = True
# Continue playing cards until no more moves
while changed:
changed = False
# Check for left expansion
card_left = left - 1
if card_left in player_dict and player_dict[card_left] > 0:
player_dict[card_left] -= 1
if player_dict[card_left] == 0:
del player_dict[card_left]
left = card_left
has_played = True
changed = True
if turn == 0:
count_adam -= 1
else:
count_bob -= 1
# Check for right expansion
card_right = right + 1
if card_right in player_dict and player_dict[card_right] > 0:
player_dict[card_right] -= 1
if player_dict[card_right] == 0:
del player_dict[card_right]
right = card_right
has_played = True
changed = True
if turn == 0:
count_adam -= 1
else:
count_bob -= 1
# Check if current player has no cards left
if (turn == 0 and count_adam == 0) or (turn == 1 and count_bob == 0):
winner = "Adam" if turn == 0 else "Bob"
print(winner)
print(0)
return
# Update pass status
if not has_played:
if last_pass:
break
else:
last_pass = True
else:
last_pass = False
# Switch turn
turn = 1 - turn
# Determine the winner based on remaining cards
if count_adam < count_bob:
print("Adam")
print(count_adam)
elif count_bob < count_adam:
print("Bob")
print(count_bob)
else:
print("Draw")
print(-1)
if __name__ == '__main__':
main()