[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ1] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 14888 - ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ

keyneneยท2022๋…„ 11์›” 3์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
20/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ1] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 14888 - ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ

๐Ÿ“œ๋ฌธ์ œ





DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)

์ž„์˜์˜ ํ•œ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
#์žฌ๊ท€ #์ˆœํ™˜ #๋ฌดํ•œ๋ฃจํ”„ํƒˆ์ถœ #Back-Tracking


๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

์‚ฌ์‹ค DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด ๋ฌธ์ œ๋กœ ์ฒ˜์Œ ์ ‘ํ–ˆ๋Š”๋ฐ ์ด์ œ์•ผ ์ •๋ฆฌ๊ฐ€ ๋˜์–ด ํฌ์ŠคํŒ…ํ•œ๋‹ค.

์šฐ์„  n,a(์ˆ˜์—ด),f(์—ฐ์‚ฐ์ž)๋ฅผ ์ €์žฅํ•˜๊ณ , dfs()ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ž
dfs()ํ•จ์ˆ˜๋Š” ๋ชจ๋“  ์—ฐ์‚ฐ์ž๋“ค์„ ๋ชจ๋‘ ๋‹ค ๋ฐฉ๋ฌธํ•˜์—ฌ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ ,
์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’์„ max, minํ•จ์ˆ˜๋กœ ์ €์žฅํ•˜์—ฌ ์ถœ๋ ฅํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. n,a(์ˆ˜์—ด),f(์—ฐ์‚ฐ์ž๋ฆฌ์ŠคํŠธ)๋ฅผ ์ €์žฅ ๋ฐ minR, maxR์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , dfs()ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ž

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

maxR = -1e9  #์ตœ๋Œ“๊ฐ’ ์ €์žฅ์šฉ
minR = 1e9   #์ตœ์†Ÿ๊ฐ’ ์ €์žฅ์šฉ

def dfs(idx, res, sum, sub, mul, div):
    global maxR, minR

    if idx == n:
        maxR = max(res, maxR)
        minR = min(res, minR)
        return

    if sum:
        dfs(idx+1, res+a[idx], sum-1, sub, mul, div)
    if sub:
        dfs(idx+1, res-a[idx], sum, sub-1, mul, div)
    if mul:
        dfs(idx+1, res*a[idx], sum, sub, mul-1, div)
    if div:
        dfs(idx+1, int(res/a[idx]), sum, sub, mul, div-1)

n = int(input().rstrip())
a = list(map(int, input().split()))
f = list(map(int, input().split()))


dfs(1, a[0], f[0], f[1], f[2], f[3])
print(maxR)
print(minR)

โœ๏ธ1. n, a(์ˆ˜์—ด), f(์—ฐ์‚ฐ์ž๋ฆฌ์ŠคํŠธ), dfs()ํ•จ์ˆ˜ ๊ตฌํ˜„

import sys
input = sys.stdin.readline

maxR = -1e9
minR = 1e9

#res๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ๊ณ ๋ฐ›์œผ๋ฉด์„œ maxR, minR์— ๊ฐ’ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ
def dfs(idx, res, sum, sub, mul, div):
    global maxR, minR

    if idx == n: #๋ชจ๋“  ์ˆ˜๋ฅผ ๋‹ค ๋ฐฉ๋ฌธ ์‹œ res๋ฅผ minR, maxR์— ๊ฐ๊ฐ ์ €์žฅ (ํƒˆ์ถœ)
        maxR = max(res, maxR)
        minR = min(res, minR)
        return

    if sum:
        dfs(idx+1, res+a[idx], sum-1, sub, mul, div)
    if sub:
        dfs(idx+1, res-a[idx], sum, sub-1, mul, div)
    if mul:
        dfs(idx+1, res*a[idx], sum, sub, mul-1, div)
    if div:
        dfs(idx+1, int(res/a[idx]), sum, sub, mul, div-1)

n = int(input().rstrip())
a = list(map(int, input().split()))
f = list(map(int, input().split()))


dfs(1, a[0], f[0], f[1], f[2], f[3])
print(maxR)
print(minR)

๐Ÿ“š๋™์ž‘์›๋ฆฌ ๋ฐ ์ •๋ฆฌ

โ“๋™์ž‘์›๋ฆฌ

ex)
n = 6
a = 1 2 3 4 5 6
f = 2 1 1 1

def dfs(idx, res, sum, sub, mul, div):
    global maxR, minR

    if idx == n:
        maxR = max(res, maxR)
        minR = min(res, minR)
        return

    if sum:
        dfs(idx+1, res+a[idx], sum-1, sub, mul, div)
    if sub:
        dfs(idx+1, res-a[idx], sum, sub-1, mul, div)
    if mul:
        dfs(idx+1, res*a[idx], sum, sub, mul-1, div)
    if div:
        dfs(idx+1, int(res/a[idx]), sum, sub, mul, div-1)

๋™์ž‘์›๋ฆฌ (ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ฆฌ)

์ •์˜ : def dfs(idx, res, sum, sub, mul, div):
ํ˜ธ์ถœ : dfs(1, a[0], f[0], f[1], f[2], f[3])
     ์ฆ‰, dfs(1, 1, 2, 1, 1, 1)

1. ์šฐ์„  idx๊ฐ’์„ 1๋กœ ๋„˜๊ธฐ๊ณ , dfs()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ "idx+1"๊ฐ’์„ ๋„˜๊ธด๋‹ค. 
   (idx++์‹œํ‚ค๋ฉด์„œ "idx == n" ๋•Œ ์ข…๋ฃŒ์‹œํ‚ฌ๊บผ๋‹ˆ๊นŒ!)
   
2. res๊ฐ’์œผ๋กœ a[0]๋ฅผ ๋„˜๊ธฐ๊ณ , dfs()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค res์™€ ์—ฐ์‚ฐ์ž๋งˆ๋‹ค์˜ ๊ณ„์‚ฐ์„ ํ•ด์ค€๋‹ค.
   (a[0]์ผ ๋•Œ ์—ฐ์‚ฐ์ž๊ฐ€ +๋ฉด, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ a[0]+a[idx]ํ•ด์ฃผ๋Š”๋ฐ 
    ์ด ๋–„ idx=idx+1์ด๋ฏ€๋กœ, res์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ "a[0]+a[1]"๋ฅผ ๋„˜๊ธฐ๊ฒŒ ๋œ๋‹ค.
    ๊ทธ ๋‹ค์Œ ํ˜ธ์ถœ์€ res+a[2], ๊ทธ๋‹ค์Œ์€ res+a[3] ..... )
    
3. 3๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ถ€ํ„ฐ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ +, -, *, / ์ˆœ์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
   ๋‚˜๋„ ์ด ๋ถ€๋ถ„์ด ์ฐธ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ์ด๊ฒŒ DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ •์„ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ธฐ ์ข‹๋‹ค!
   
    n = 6
	a = 1 2 3 4 5 6
	f = 2 1 1 1
    
      ๐Ÿ‘‰๐Ÿปdfs(1, 1+2, 1, 1, 1, 1) sum๋™์ž‘:a[0]+a[1]    res = 3
      ๐Ÿ‘‰๐Ÿปdfs(2, 3+3, 0, 1, 1, 1) sum๋™์ž‘:res+a[2]     res = 6
      ๐Ÿ‘‰๐Ÿปdfs(3, 6-4, 0, 0, 1, 1) sub๋™์ž‘:res-a[3]     res = 2
      ๐Ÿ‘‰๐Ÿปdfs(4, 2*5, 0, 0, 0, 1) mul๋™์ž‘:res*a[4]     res = 10
      ๐Ÿ‘‰๐Ÿปdfs(5, 10/6, 0, 0, 0, 0) div๋™์ž‘:res-a[5]     res = 1
      ๐Ÿ‘‰๐Ÿปidx == n์ด๋ฏ€๋กœ res๊ฐ’ minR, maxR์— ์ €์žฅ

๋‹จ, ์ด๊ฒŒ ์ฒซ ๋ฒˆ์งธ ๋™์ž‘์ด๋ผ๋Š” ์ ์ด๋‹ค!
div๊นŒ์ง€ ๋™์ž‘ํ•œ dfs()๊ฐ€ "return"๊ฐ’์€ ์–ด๋””๋กœ ์ „๋‹ฌ๋ ๊นŒ?

      ๐Ÿ‘‰๐Ÿป๋ฐ”๋กœ, div ์ด์ „์˜ mul์— dfs(4, 2*5, 0, 0, 0, 1) ๋กœ ๋‚˜์˜จ๋‹ค!
      
      ๐Ÿ‘‰๐Ÿป๊ทธ ํ›„, mul ์ด์ „์˜ sub์— dfs(3, 6-4, 0, 0, 1, 1) ๋กœ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ
        ์—ฌ๊ธฐ์„œ if mul:์„ ํƒˆ์ถœํ•œ ์ƒํ™ฉ์ด๋ฏ€๋กœ, if div:๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜์–ด
        dfs(4, 2/5, 0, 0, 1, 0) div ๋จผ์ € ๋™์ž‘ํ•˜์—ฌ res = 0์ด ๋˜๊ณ 
        dfs(5, 0*6, 0, 0, 0, 0) mul์ด ๋™์ž‘ํ•˜์—ฌ res = 0์ด ๋˜์–ด return๋œ๋‹ค.
        
      ๐Ÿ‘‰๐Ÿป๊ทธ ํ›„, sub ์ด์ „์˜ sum์— dfs(2, 3+3, 0, 1, 1, 1) ๋กœ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ,
        ์—ฌ๊ธฐ์„œ ๋˜ if sub:๋ฅผ ํƒˆ์ถœํ•œ ์ƒํ™ฉ์ด๋ฏ€๋กœ if mul:์„ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜์–ด,
        ๊ณฑํ•˜๊ธฐ > ๋‚˜๋ˆ„๊ธฐ > ๋นผ๊ธฐ ์ˆœ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

์ด๋Ÿฐ์‹์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฐฉ๋ฌธํ•˜์—ฌ idx == n์ผ ๋•Œ๋งˆ๋‹ค max์™€ minํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด
maxR, minR์„ ๋„์ถœํ•ด๋‚ด๋Š” ๊ฒƒ์ด ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์ด๋‹ค.


์ •๋ฆฌ

  1. DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ„์—์„œ ์˜ˆ์ƒํ–ˆ๋‹ค์‹œํ”ผ ์žฌ๊ท€/์ˆœํ™˜๊ตฌ์กฐ๋ฅผ ์ต์ˆ™ํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  2. dfs()๋ฅผ ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ return ํ›„ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€
    ์„ค๊ณ„ํ•˜๊ณ  ๊ตฌํ˜„ํ•ด์•ผ ์›ํ•˜๋Š” ์ง€์ ์—์„œ ํƒˆ์ถœ์„ ํ•˜์—ฌ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์ง€์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
  3. ์žฌ๊ท€/์ˆœํ™˜ ๋ฌธ์ œ๋ฅผ ์—ฐ์Šตํ•˜์ž,,
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€