Algorithm Problem/Python

[python] SWEA - 10761. ์‹ ๋ขฐ

deo2kim 2020. 11. 8. 22:21
๋ฐ˜์‘ํ˜•

๐Ÿค”๋ฌธ์ œ ํ•ด๊ฒฐ

  • lv3 | ์‹œ๋ฎฌ๋ ˆ์ด์…˜

 

๐Ÿ’จ ์˜ˆ๋ฅผ ๋“ค์–ด B๊ฐ€ ์›€์ง์ด๋ฉด์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋™์•ˆ O๋Š” ์›€์ง์ผ ์ˆœ ์žˆ์–ด๋„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ์ˆœ ์—†๋‹ค. (๋ฐ˜๋Œ€๋„ ๋งˆ์ฐฌ๊ฐ€์ง€)

๐Ÿ’จ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ!

๐Ÿ’จ ํ•˜์ง€๋งŒ ์›€์ง์ผ ์ˆœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฏธ๋ฆฌ ๋ฒ„ํŠผ์œผ๋กœ ์ด๋™ํ•ด์žˆ์–ด๋„ ๋œ๋‹ค.

๐Ÿ’จ ํด๋ž˜์Šค ํ•œ๋ฒˆ ์จ๋ณด๊ณ  ์‹ถ์–ด์„œ ์จ๋ด„. ๊ตณ์ด ์“ธ ํ•„์š”๋Š” ์—†๋‹ค. (ํด๋ž˜์Šค๋„ ๋ฐ˜๋งŒ์“ด๊ฑฐ๋ผ)

 

๐Ÿ’ป์†Œ์Šค ์ฝ”๋“œ

class Robot:
    def __init__(self):
        self.location = 1
        self.time = 0

    def status(self):
        print(f'์œ„์น˜:{self.location}, ์‹œ๊ฐ„:{self.time}')


def operate(robot, button):  # ํ˜„์žฌ์œ„์น˜์™€ ๋ˆŒ๋Ÿฌ์•ผ ๋˜๋Š” ๋ฒ„ํŠผ
    global time
    # ์‹œ๊ฐ„์ด ๊ฐ™์œผ๋ฉด ๋กœ๋ด‡์ด๋™
    if time == robot.time:
        robot.time += abs(robot.location - button) + 1
        robot.location = button
        time = robot.time
    else:
        if time - robot.time > abs(robot.location - button):  # ์‹œ๊ฐ„์ด ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด ๋กœ๋ด‡์€ ์ด๋ฏธ ์ด๋™ํ–ˆ๋‹ค
            time += 1  # ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋Š” ์‹œ๊ฐ„
            robot.time = time
            robot.location = button
        else:  # ์‹œ๊ฐ„์ด ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋กœ๋ด‡์€ ์‹œ๊ฐ„์ฐจ์ด๋งŒํผ๋งŒ ์ด๋™ ํ›„ ๋‚˜๋จธ์ง€ ์ด๋™
            time += abs(robot.location - button) - (time - robot.time) + 1
            robot.location = button
            robot.time = time
    return


for tc in range(int(input())):
    tmp = input().split()
    N = int(tmp[0])
    orders = tmp[1:]
    orange = Robot()
    blue = Robot()
    time = 0
    for i in range(N):
        r, o = orders[i * 2], orders[i * 2 + 1]
        if r == 'O':
            operate(orange, int(o))
        else:
            operate(blue, int(o))

    print(f'#{tc + 1} {time}')
 

๐Ÿ“•๋ฌธ์ œ ํ™•์ธ

์ถœ์ฒ˜: SW Expert Academy

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

๋ฐ˜์‘ํ˜•