[BOJ][๐ŸŸก2][๋ฐฑ์ค€#17825] ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #17825


๋ฌธ์ œ

์ฃผ์‚ฌ์œ„ ์œท๋†€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒŒ์ž„ํŒ์—์„œ ํ•˜๋Š” ๊ฒŒ์ž„์ด๋‹ค.

์ฒ˜์Œ์—๋Š” ์‹œ์ž‘ ์นธ์— ๋ง 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. ๋ง์€ ๊ฒŒ์ž„ํŒ์— ๊ทธ๋ ค์ง„ ํ™”์‚ดํ‘œ์˜ ๋ฐฉํ–ฅ๋Œ€๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ง์ด ํŒŒ๋ž€์ƒ‰ ์นธ์—์„œ ์ด๋™์„ ์‹œ์ž‘ํ•˜๋ฉด ํŒŒ๋ž€์ƒ‰ ํ™”์‚ดํ‘œ๋ฅผ ํƒ€์•ผ ํ•˜๊ณ , ์ด๋™ํ•˜๋Š” ๋„์ค‘์ด๊ฑฐ๋‚˜ ํŒŒ๋ž€์ƒ‰์ด ์•„๋‹Œ ์นธ์—์„œ ์ด๋™์„ ์‹œ์ž‘ํ•˜๋ฉด ๋นจ๊ฐ„์ƒ‰ ํ™”์‚ดํ‘œ๋ฅผ ํƒ€์•ผ ํ•œ๋‹ค. ๋ง์ด ๋„์ฐฉ ์นธ์œผ๋กœ ์ด๋™ํ•˜๋ฉด ์ฃผ์‚ฌ์œ„์— ๋‚˜์˜จ ์ˆ˜์™€ ๊ด€๊ณ„ ์—†์ด ์ด๋™์„ ๋งˆ์นœ๋‹ค. ๊ฒŒ์ž„์€ 10๊ฐœ์˜ ํ„ด์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ๋งค ํ„ด๋งˆ๋‹ค 1๋ถ€ํ„ฐ 5๊นŒ์ง€ ํ•œ ๋ฉด์— ํ•˜๋‚˜์”ฉ ์ ํ˜€์žˆ๋Š” 5๋ฉด์ฒด ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฌ๊ณ , ๋„์ฐฉ ์นธ์— ์žˆ์ง€ ์•Š์€ ๋ง์„ ํ•˜๋‚˜ ๊ณจ๋ผ ์ฃผ์‚ฌ์œ„์— ๋‚˜์˜จ ์ˆ˜๋งŒํผ ์ด๋™์‹œํ‚จ๋‹ค. ๋ง์ด ์ด๋™์„ ๋งˆ์น˜๋Š” ์นธ์— ๋‹ค๋ฅธ ๋ง์ด ์žˆ์œผ๋ฉด ๊ทธ ๋ง์€ ๊ณ ๋ฅผ ์ˆ˜ ์—†๋‹ค. ๋‹จ, ์ด๋™์„ ๋งˆ์น˜๋Š” ์นธ์ด ๋„์ฐฉ ์นธ์ด๋ฉด ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๋ง์ด ์ด๋™์„ ๋งˆ์น  ๋•Œ๋งˆ๋‹ค ์นธ์— ์ ํ˜€์žˆ๋Š” ์ˆ˜๊ฐ€ ์ ์ˆ˜์— ์ถ”๊ฐ€๋œ๋‹ค.

์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜ฌ ์ˆ˜ 10๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ์„ ๋•Œ, ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜ฌ ์ˆ˜ 10๊ฐœ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

1 2 3 4 1 2 3 4 1 2


์ถœ๋ ฅ

190


์˜ˆ์ œ 2

์ž…๋ ฅ

1 1 1 1 1 1 1 1 1 1


์ถœ๋ ฅ

133


์˜ˆ์ œ 3

์ž…๋ ฅ

5 1 2 3 4 5 5 3 2 4


์ถœ๋ ฅ

214


์˜ˆ์ œ 4

์ž…๋ ฅ

5 5 5 5 5 5 5 5 5 5


์ถœ๋ ฅ

130


My Sol

def main():
    def make_vals():
        vals = [0]*32
        for i in range(21):
            vals[i] = 2*i
        vals[21], vals[22], vals[23] = 13, 16, 19
        vals[24], vals[25] = 22, 24
        vals[26], vals[27], vals[28] = 28, 27, 26
        vals[29], vals[30], vals[31] = 25, 30, 35
        return vals

    def make_children():
        children = {}
        for n in range(20):
            children[n] = n+1
        for a, b in ((21, 23), (24, 25), (26, 28), (29, 31)):
            for n in range(a, b):
                children[n] = n+1
        children[23] = children[25] = children[28] = 29
        children[31] = 20
        children[20] = -1

        special_children = {5: 21, 10: 24, 15: 26}
        return children, special_children

    def move(move_cnt, acc, pos_list, pos_set):
        def move_next(is_special, cur_max_acc):
            ret = cur_max_acc
            step_cnt = 1
            cur_pos = special_children[p] if is_special else children[p]
            if cur_pos != -1:
                while step_cnt < cur_move:
                    cur_pos = children[cur_pos]
                    if cur_pos==-1: break
                    step_cnt += 1

            new_pos_list = pos_list[:]
            new_pos_list[pi] = cur_pos
            new_pos_set = pos_set - {p}
            if cur_pos==-1:
                new_pos_list.pop(pi)
                ret = max(ret, move(move_cnt+1, acc, new_pos_list, new_pos_set))
            else:
                if cur_pos in pos_set: return 0
                ret = max(ret, move(move_cnt+1, acc+vals[cur_pos], new_pos_list, new_pos_set|{cur_pos}))
            return ret

        if move_cnt==10: return acc

        cur_max_acc = acc
        cur_move = moves[move_cnt]
        for pi in range(len(pos_list)):
            p = pos_list[pi]
            is_special = p in special_children_set
            cur_max_acc = max(cur_max_acc, move_next(is_special, cur_max_acc))
        return cur_max_acc


    moves = list(map(int, input().split()))
    vals = make_vals()
    children, special_children = make_children()
    special_children_set = {5, 10, 15}
    return move(0, 0, [0, 0, 0, 0], {0})

print(main())

์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์จ์•ผํ•˜๋‚˜ ๊ฐ์ด ์•ˆ ์™”๋Š”๋ฐ ์‚ฌ์‹ค์ƒ ์Œฉ๋…ธ๊ฐ€๋‹ค ๊ตฌํ˜„ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค. DFS๋ฅผ ํ™œ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

  1. ์œท๋†€์ดํŒ๊ณผ ๊ทœ์น™์„ ํŠธ๋ฆฌ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด ์ž์‹์„ ๋ฐ”๋ผ๋ณด๋„๋ก ํ–ˆ๋‹ค. ๊ฐ’ ์ž์ฒด๋ฅผ ๋ฒˆํ˜ธ๋กœ ํ•˜๊ธฐ์—๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์–ด์„œ ๊ฐ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๊ณ  ํ•ด๋‹น ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์ง€๋Š” ๊ฐ’์„ ๋”ฐ๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ make_children๊ณผ make_vals ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถ„๋ฆฌํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  2. DFS ํ•จ์ˆ˜ move๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค. parameter๋กœ๋Š” ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” move_cnt, ๋ˆ„์ ๊ฐ’์ธ acc, ๋ง๋“ค์˜ ์œ„์น˜ ์ •๋ณด ๋ฆฌ์ŠคํŠธ pos_list, ์œ„์น˜ ์ •๋ณด set pos_set์ด๋‹ค.
  3. ํ˜„์žฌ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์Šคํ… ์ˆ˜๋ฅผ cur_move๋กœ ๋‘์—ˆ๋‹ค. ์ด๋Š” ํ˜„์žฌ ์ด๋™ ํšŸ์ˆ˜ move_cnt๋ฅผ ์ด๋™์Šคํ… ๋ฐฐ์—ด moves์˜ ์ธ๋ฑ์Šค๋กœ ํ•˜์—ฌ ๊ตฌํ•œ๋‹ค.
  4. ๊ฐ ๋ง๋“ค์˜ ์ •๋ณด์ธ pos_list์˜ ๊ฐ ๋ง๋“ค์„ ์›€์ง์ธ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋งŒ์•ฝ ํŒŒ๋ž€์ƒ‰ ์œ„์น˜์— ์žˆ์–ด์„œ ๋” ๋น ๋ฅด๊ฒŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด specialํ•˜๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋ฏธ find_children ํ•จ์ˆ˜์—์„œ specialํ•œ ๊ฒฝ์šฐ์˜ ์œ„์น˜์™€ children ์ •๋ณด๋ฅผ ๊ตฌํ•ด๋‘์—ˆ๋‹ค.
  5. ํ˜„์žฌ ๋ง์˜ ์œ„์น˜๊ฐ€ specialํ•œ ์œ„์น˜์— ์žˆ๋Š”์ง€, ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ์„œ ์ด๋™ children์ด ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ์ด๋ฅผ ํ™•์ธํ•œ๋‹ค. move_next ํ•จ์ˆ˜์—์„œ ๋‹ค์Œ์„ ์ง„ํ–‰ํ•œ๋‹ค.
  6. move_next ํ•จ์ˆ˜๋Š” ์ด๋™ ์Šคํ… ํšŸ์ˆ˜๋งŒํผ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ ์ด๋™ํ•˜์—ฌ ๋„์ฐฉ ์ง€์ ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด๊ฒƒ์ด ๊ฐ’์ด 40์ธ 20๋ฒˆ ๋…ธ๋“œ๋ฅผ ๋„˜๋Š”๋‹ค๋ฉด ๋„์ฐฉ ์ง€์ ์— ๋„์ฐฉํ•œ ๊ฒƒ์ด๋‹ค. ๋„์ฐฉ์ง€์ ์˜ ๋ฒˆํ˜ธ๋ฅผ -1๋กœ ์„ค์ •ํ–ˆ๋‹ค.
  7. ๋ชจ๋“  ์ด๋™์„ ๋งˆ์น˜๊ณ  ํ˜„์žฌ ์œ„์น˜๊ฐ€ -1์ด๋ผ๋ฉด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ๋ง์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— new_pos_list์—์„œ ๋นผ์ค€๋‹ค.
  8. ๋งŒ์•ฝ ๋„์ฐฉ์ ์ด -1์ด ์•„๋‹ˆ๋ผ๋ฉด, ์ด๋ฏธ ๊ทธ๊ณณ์— ๋ง์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ด๊ฒƒ์€ pos_set์„ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด ๋ฌด์˜๋ฏธํ•œ ์ด๋™์ด๋ฏ€๋กœ ํ˜„์žฌ ret์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์–ด์ฐจํ”ผ max ํ•จ์ˆ˜์—์„œ ๋ฌด์˜๋ฏธํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋œ๋‹ค.
  9. ๋งŒ์•ฝ ํ˜„ ์œ„์น˜๊ฐ€ ๋„์ฐฉ์ ์ด ์•„๋‹ˆ๋ฉฐ ๋ง์ด ์—†๋‹ค๋ฉด ํ˜„ ์œ„์น˜๋ฅผ pos_list์™€ pos_set ์— ์ƒˆ๋กœ ์ƒˆ๊ธด new_pos_list์™€ new_pos_set์„ ๋‹ค์Œ parameters๋กœ ํ•˜์—ฌ move ํ•จ์ˆ˜๋ฅผ ์žฌ๊ฐœํ•œ๋‹ค. ์ด๋•Œ ํ˜„์žฌ ์œ„์น˜์˜ ๊ฐ’์„ ๋ˆ„์ ๊ฐ’์— ๋”ํ•˜๊ณ , ์ด๋™ํšŸ์ˆ˜๋ฅผ 1 ๋Š˜๋ ค์ฃผ๋„๋ก ํ•œ๋‹ค.
  10. move_cnt๊ฐ€ 10์ด ๋˜๋ฉด ์ด ์‹œ์ ์˜ acc๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ชจ๋‘ ์ด๋™์˜ ์ข…์ ์„ ์ฐ์€ ๊ฒฐ๊ณผ๋“ค์˜ ์ตœ๋Œ“๊ฐ’์„ ๋‹ค์‹œ ์ƒ์œ„๋กœ ์˜ฌ๋ฆฌ๋ฏ€๋กœ main์—์„œ๋Š” ์ด ํ•จ์ˆ˜์˜ ์‹œ์ž‘์ ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

# empty

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ