ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€ 14891] ํ†ฑ๋‹ˆ๋ฐ”ํ€ด (Python)

์€์ง„ 2021. 8. 9. 20:56

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[๋ฐฑ์ค€ 14891] ํ†ฑ๋‹ˆ๋ฐ”ํ€ด (Python)
๋ฐฑ์ค€


โœ๏ธIdea Sketch

2021-08-09

1. ๋ฌธ์ œ ์ดํ•ดํ•˜๊ธฐ

  • ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋Š” ํ•œ ์นธ ์ด๋™์„ ๊ธฐ์ค€์œผ๋กœ, K๋ฒˆ ํšŒ์ „ํ•œ๋‹ค.
  • ์‹œ๊ณ„ or ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•œ๋‹ค.
  • A๊ฐ€ ํšŒ์ „ํ•˜๊ธฐ ์ „
    1. B์™€ ๊ทน์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ, B๋Š” A์™€ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•œ๋‹ค.
    2. B์™€ ๊ทน์ด ๊ฐ™์€ ๊ฒฝ์šฐ, B๋Š” ํšŒ์ „ํ•˜์ง€ ์•Š๋Š”๋‹ค.

2. ์ž…๋ ฅ๊ฐ’ ์‚ดํŽด๋ณด๊ธฐ

  • 12์‹œ๋ถ€ํ„ฐ ์‹œ๊ณ„๋ฐฉํ–ฅ
    • 0์€ N๊ทน, 1์€ S๊ทน
  • ํšŒ์ „ ํšŸ์ˆ˜ K
  • ํšŒ์ „ํ•  ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ๋ฒˆํ˜ธ(num) ๋ฐ ํšŒ์ „๋ฐฉํ–ฅ
    1. 1์€ ์‹œ๊ณ„๋ฐฉํ–ฅ
    2. -1๋Š” ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ


3. ์žฌ๊ท€์  ๋กœ์ง

  • ์˜ˆ์ œ1 ์ฐธ๊ณ ) ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 3์„ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œ์ผœ๋ณด์ž.
  1. ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 3๊ณผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 4๋ฅผ ์‚ดํŽด๋ณด์ž. recursion()

    • graph[3][2]์™€ graph[4][6]์˜ ๋‘ ๊ทน์ด
      1. ๋‹ค๋ฅธ ๊ฒฝ์šฐ, ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 4๋Š” ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•œ๋‹ค. handle_rotation()
      2. ๊ฐ™์€ ๊ฒฝ์šฐ, ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 4๋Š” ํšŒ์ „ํ•˜์ง€ ์•Š๋Š”๋‹ค.
      • ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 4๋ฅผ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œ์ผœ๋ณด์ž.
  2. ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 3๊ณผ ์™ผ์ชฝ์œผ๋กœ ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 2๋ฅผ ์‚ดํŽด๋ณด์ž. recursion()

    • graph[3][6]๊ณผ graph[2][2]์˜ ๋‘ ๊ทน์ด
      1. ๋‹ค๋ฅธ ๊ฒฝ์šฐ, ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 2๋Š” ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•œ๋‹ค. handle_rotation()
      2. ๊ฐ™์€ ๊ฒฝ์šฐ, ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 2๋Š” ํšŒ์ „ํ•˜์ง€ ์•Š๋Š”๋‹ค.
      • ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 2๋ฅผ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œ์ผœ๋ณด์ž.


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-08-09 72ms ํ†ต๊ณผ

import sys

def recursion(num, rotation, vs):  # ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ๋ฒˆํ˜ธ, ํšŒ์ „๋ฐฉํ–ฅ, ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด (1 or -1)
  if (0<num+vs<=4):
    if vs == 1:  # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด
      if graph[num][2] != graph[num+vs][6]:
        recursion(num+vs, -rotation, vs)
        handle_rotation(num+vs, -rotation)
    else:  # ์™ผ์ชฝ์œผ๋กœ ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด
      if graph[num][6] != graph[num+vs][2]:
        recursion(num+vs, -rotation, vs)
        handle_rotation(num+vs, -rotation)

def handle_rotation(num, rotation):
  if rotation == 1:  # ์‹œ๊ณ„๋ฐฉํ–ฅ
    graph[num].insert(0, graph[num].pop())
  else:  # ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ
    graph[num].append(graph[num][0])
    del graph[num][0]

if __name__ == "__main__":
  graph = [[0, 0, 0, 0, 0, 0, 0, 0]]
  for _ in range(4):
    graph.append(list(map(int, sys.stdin.readline().rstrip())))

  k = int(input())
  for _ in range(k):
    num, rotation = map(int, sys.stdin.readline().rstrip().split())
    recursion(num, rotation, 1)
    recursion(num, rotation, -1)
    handle_rotation(num, rotation)

  result = graph[1][0]
  result += graph[2][0]*2
  result += graph[3][0]*4
  result += graph[4][0]*8
  print(result)