CS/알고리즘_문제풀이(파이썬)

알파윷 - E

Jedy_Kim 2021. 8. 11. 14:29
728x90

문제

T개의 윷을 던진 상황이 주어질때, 각 상황에서 얻어지는 점수 중 최댓값을 알아보자.

각 상황에서 말 4개가 움직인다. 얻은 점수의 합이란, 각 말이 얻은 점수의 총 합을 의미하며 각 말은 마지막에 밟았던 칸의 점수를 자신의 점수로 갖는다.

말의 이동 시작점은 Start 이며, 도착지점은 End 이다. 만약, 이동해야하는 칸이 도착 지점을 넘어서거나, 도착 지점을 밟았을 경우 도착 지점의 점수를 얻고 해당 말은 판에서 제외된다. 제외된 말은 더 이상 이동할 수 없다. 예를 들어, 50점과 75점 칸을 밟을 경우, 화살표가 가리키는 방향으로 움직여야한다. 기본적으로 흰색 칸이 있는 경로로 이동한다.

이동하려고 하는 칸에 다른 말이 있는 경우 이동할 수 없다. 해당 상황의 결과값은 "-1"이다.

입력

첫 번째 줄에 상황의 개수 T가 주어진다. 두 번째 줄부터 T x 2개의 줄에 걸쳐 윷을 던진 상황이 주어진다. 각 상황의 첫 번째 줄에 이동하게될 칸의 수가 10개 주어진다. 그리고 두 번째 줄에 각 이동하게될 칸의 수가 몇 번 말이 될지 주어진다.(2 ≤ T ≤ 10, 1 ≤ 이동하는 칸의 수 ≤ 5, 1 ≤ 말의 번호 ≤ 4)

출력

T개의 상황에서 얻어지는 점수 중 최댓값을 알아보자. 만약 주어진 모든 상황에서 얻은 결과값이 -1이라면 "-1"을 출력한다.

입력의 예

4
2 3 1 2 4 5 2 3 4 1 
1 1 2 3 4 1 2 2 3 1 
5 5 5 5 5 5 5 5 5 5 
1 2 3 4 1 2 3 4 1 2 
5 3 4 5 3 4 5 3 4 5 
1 1 1 2 2 2 3 3 3 4 
3 4 3 2 1 2 3 4 3 2 
1 2 2 3 4 3 1 4 4 1 

출력의 예

3050

 

#코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from collections import defaultdict
import sys
 
def runThisGame(move, player):
  global player_arr, main_board, sub_board_idx, moving
  player_cnt   = defaultdict(int)
  isPossible   = True
  
  for p in player:
    player_cnt[p] = 0
    
  
  for m, p in zip(move, player):
    for i in range(11):
      flag = False
      if player_cnt[p] == sub_board_idx[i][0]:
        flag = True
        break
    
    if flag:
      player_cnt[p] = moving[player_cnt[p]][m]
    else:
      player_cnt[p] += m
      if player_cnt[p] >= 21: player_cnt[p] = 21
    
    temp_dic = defaultdict(int)
    for k, v in player_cnt.items():
      if v > 0:
        temp_dic[v] += 1 
      if temp_dic[v] > 1 and player_cnt[k] != 21
        isPossible = False 
        break  
  
  if isPossible:
    sum_val = 0
    for k, v in player_cnt.items():
      sum_val += main_board[v]
    return sum_val
  else:
    return -1
   
 
if __name__ == "__main__":
  input = sys.stdin.readline
  
  main_board     = [0510152050303540451005560657075808590955001000275250300150175150125350400]
  sub_board_idx  = [
    [52223242526],
    [222324252615],
    [232425261516],
    [242930202121],
    [252615161718],
    [261516171819],
    [102728242930],
    [272824293020],
    [282429302021],
    [293020212121],
    [302021212121]
  ]
  
  moving = [[-1* 6 for _ in range(35)]
  for i in range(11):
    for j in range(16):
      moving[sub_board_idx[i][0]][j] = sub_board_idx[i][j]
      
  T          = int(input())  
  move_arr   = []
  player_arr = []
  my_max     = -int(1e9)
  
  for i in range((T * 2)):
    if i % 2 == 0:
      move_arr.append(list(map(int, input().split())))
    else:
      player_arr.append(list(map(int, input().split())))
  
  for move, player in zip(move_arr, player_arr):
    score = runThisGame(move, player)
    if score > my_max:
      my_max = score
      
  print(my_max)
cs

 

반응형

'CS > 알고리즘_문제풀이(파이썬)' 카테고리의 다른 글

사탕 담기  (0) 2021.08.13
세 소수의 합  (0) 2021.08.13
알파윷 - D  (0) 2021.08.11
퇴사  (0) 2021.08.10
알파윷 - C  (0) 2021.08.09