ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

15662๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด (2)

์ด 8๊ฐœ์˜ ํ†ฑ๋‹ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด T๊ฐœ๊ฐ€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ๋˜, ํ†ฑ๋‹ˆ๋Š” N๊ทน ๋˜๋Š” S๊ทน ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋‹ค. ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์—๋Š” ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋Š”๋ฐ, ๊ฐ€์žฅ ์™ผ์ชฝ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ๋‹ค๋ฃฌ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ๋ช‡ ๊ฐ€์ง€ ์กฐ๊ฑด๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด์ „์˜ ๋ฌธ์ œ๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์—ˆ์ง€๋งŒ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ์ˆ˜๊ฐ€ T๋กœ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋˜ํ•œ ์ถœ๋ ฅ์€ K๋ฒˆ ํšŒ์ „์‹œํ‚จ ํ›„ 12์‹œ ๋ฐฉํ–ฅ์ด S๊ทน์ธ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def move_gear(idx, cur_dir):
    global cur_right, cur_left, gears
    origin_dir = cur_dir

    # ํ˜„์žฌ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก์˜ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋“ค ํ™•์ธ.
    for i in reversed(range(idx)):
        if cur_right != gears[i][LEFT]:
            cur_right = gears[i][RIGHT]
            gears[i].rotate(cur_dir * -1)
            cur_dir *= -1
        else:
            break

    cur_dir = origin_dir
    # ํ˜„์žฌ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์ธก์˜ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋“ค ํ™•์ธ.
    for i in range(idx + 1, t):
        if gears[i][RIGHT] != cur_left:
            cur_left = gears[i][LEFT]
            gears[i].rotate(cur_dir * -1)
            cur_dir *= -1
        else:
            break


if __name__ == '__main__':
    LEFT, RIGHT = 2, 6
    t = int(stdin.readline())
    gears = [deque(map(int, stdin.readline().strip())) for _ in range(t)]
    k = int(stdin.readline())
    orders = [list(map(int, stdin.readline().split())) for _ in range(k)]

    for gear_idx, direction in orders:
        gear_idx -= 1
        cur_left, cur_right = gears[gear_idx][LEFT], gears[gear_idx][RIGHT]
        gears[gear_idx].rotate(direction)
        move_gear(gear_idx, direction)

    print(sum([1 if gear[0] else 0 for delta, gear in enumerate(gears)]))
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€