공부/코딩테스트

백준 6954번 Card Game. Python 파이썬 답안, 백준 코딩테스트 파이썬 풀이

혼밥맨 2024. 6. 23. 00:07
반응형

백준 6954번 Card Game. Python 파이썬 답안, 백준 코딩테스트 파이썬 풀이

 

문제

Write a program that will keep score for a simple two-player game, played with a deck of cards. There are 52 cards in the deck; four of each of 13 possible names: two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace. The cards labelled jack, queen, king, and ace are collectively known as high cards.

The deck is shuffled and placed face-down on the table. Player A turns over the top card and places it on a pile; then player B turns over the top card and places it on the pile. A and B alternate until the deck is exhausted. The game is scored as follows:

  • If a player turns over an ace, with at least 4 cards remaining to be turned over, and none of the next 4 cards is a high card, that player scores 4 points
  • If a player turns over a king, with at least 3 cards remaining to be turned over, and none of the next 3 cards is a high card, that player scores 3 points
  • If a player turns over a queen, with at least 2 cards remaining to be turned over, and none of the next 2 cards is a high card, that player scores 2 points
  • If a player turns over a jack, with at least 1 card remaining to be turned over, and the next card is not a high card, that player scores 1 point

입력

The input will contain 52 lines. Each line will contain the name of a card in the deck, in lowercase letters. The first line denotes the first card to be turned over; the next line the next card; and so on.

출력

Whenever a player scores, print Player X scores n point(s). where X is the name of the player (A or B) and 𝑛 is the number of points scored (1, 2, 3, or 4). At the end of the game, print the total score for each player, on two lines:

Player A: n point(s).
Player B: m point(s).

답안

def main():
    import sys
    input = sys.stdin.read
    cards = input().split()
    
    # Define high cards
    high_cards = {'jack', 'queen', 'king', 'ace'}
    
    # Points for each high card if conditions are met
    points_map = {'ace': 4, 'king': 3, 'queen': 2, 'jack': 1}
    
    # Total scores
    score_A = 0
    score_B = 0
    
    # Process each card play
    for i in range(52):
        player = 'A' if i % 2 == 0 else 'B'
        card = cards[i]
        
        # Check if the current card can potentially score points
        if card in points_map:
            required_cards = points_map[card]  # Number of cards to check
            if i + required_cards < 52:  # Ensure there are enough cards left
                valid = True
                for j in range(1, required_cards + 1):
                    if cards[i + j] in high_cards:
                        valid = False
                        break
                if valid:
                    if player == 'A':
                        score_A += required_cards
                    else:
                        score_B += required_cards
                    print(f"Player {player} scores {required_cards} point(s).")
    
    # Output the total scores
    print(f"Player A: {score_A} point(s).")
    print(f"Player B: {score_B} point(s).")

if __name__ == "__main__":
    main()
반응형