Skip to content

Conversation

@zaqquum
Copy link
Collaborator

@zaqquum zaqquum commented Apr 8, 2025

๐ŸŒฑWIL

-์ด๋ฒˆ ์ฃผ๋Š” ๋„ค์ด๋ฒ„ ์ฝ”ํ…Œ๋ฅผ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด DFS/BFS ์œ„์ฃผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค. ์‚ฌ์‹ค ์ฒ˜์Œ์œผ๋กœ ๋งค์ผ ์ฝ”ํ…Œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ, ๋ฌธ์ œ 1๊ฐœ ํ‘ธ๋Š”๋ฐ ํ•˜๋ฃจ 3์‹œ๊ฐ„ ์ด์ƒ ์“ฐ์ด๊ธฐ๋„ ํ•ด์„œ ์‹ค๋ ฅ์— ๋Œ€ํ•œ ์กฐ๋ฐ”์‹ฌ์ด ๋งŽ์ด ์ƒ๊ฒผ๋‹ค.
์ด์ œ ๋ถ€ํ„ฐ ์ •์‹  ์ฐจ๋ฆฌ๊ณ  ์ฝ”ํ…Œ ์ค€๋น„ํ•˜์ž

๐Ÿš€์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋ฌธ์ œ ์ˆ˜: 3๊ฐœ

ํ‘ผ ๋ฌธ์ œ


๋ฐฑ์ค€ #2293 .๋™์ „ 1: DP/ ๊ณจ๋“œ5

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉํ”Œ๋กœ์šฐ (์„ ํƒ)

์ ํ™”์‹ : ```dp[lv][k] = dp[lv-1][k] + dp[lv][k-c]````

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

import sys

n , k = map(int,sys.stdin.readline().split())
coins = list()
dp = [0 for _ in range(k+1)]
for _ in range(n):
    coins.append(int(sys.stdin.readline()))
# ๊ฐ€์น˜๊ฐ€ ์ž‘์€ coin ๋ถ€ํ„ฐ ์ ์šฉ
coins.sort()
dp[0] = 1

for c in coins:
    for i in range(c, k+1) :
        dp[i] = dp[i] + dp[i-c]
print(dp[k])

๋ฐฑ์ค€ #2294 .๋™์ „ 2: DP/ ์‹ค๋ฒ„1

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉํ”Œ๋กœ์šฐ (์„ ํƒ)

์ฝ”๋“œ๋ฅผ ํ’€์ดํ•  ๋•Œ ์ ์—ˆ๋˜ ํ”Œ๋กœ์šฐ๊ฐ€ ์žˆ๋‚˜์š”?
์ ํ™”์‹ : dP[k] = min(dp์ด์ „[k] , "dp[k-c]+1 )

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

import sys

n , k  = map(int, sys.stdin.readline().split())
coins = list()
for _ in range(n):
    coins.append(int(sys.stdin.readline()))

coins.sort() 

# 2. dp ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
INF= int(1e9)
dp = [INF for _ in range(k+1)]
dp[0] = 0 
for c in coins :
    for j in range(c, k+1) :
        dp[j] = min(dp[j] , dp[j-c]+1)

#3. ์ถœ๋ ฅ - ์—…๋ฐ์ดํŠธx ๋ฉด -1 ์ถœ๋ ฅ
if dp[k] == INF : 
    print(-1)
else : 
    print(dp[k])

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค . ์•„์ดํ…œ ์ค๊ธฐ: DFS&BFS / level3

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉํ”Œ๋กœ์šฐ (์„ ํƒ)

์ฝ”๋“œ๋ฅผ ํ’€์ดํ•  ๋•Œ ์ ์—ˆ๋˜ ํ”Œ๋กœ์šฐ๊ฐ€ ์žˆ๋‚˜์š”?

  1. ์ธ์ ‘ ํ–‰๋ ฌ
    -field[x][y] = ๊ธธ์— ํ•ด๋‹นํ•˜๋Š” (x,y) ์นธ = 1 ,๊ธธ x ์ธ ์นธ์€ (x,y )= 0

-(1) ๊ฐ ์ง์‚ฌ๊ฐํ˜• ํ…Œ๋‘๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ์ขŒํ‘œ ์„ ์ถœ

  • (2) A- > B ๊ฐ ๋ฒ”์œ„์— ๊ฒน์น˜๋Š” ํ…Œ๋‘๋ฆฌ ์ขŒํ‘œ ์ œ๊ฑฐ[ํ•„ํ„ฐ๋ง]
    -> ๋‚ด๋ถ€ ๋นˆ ๊ณต๊ฐ„์€ ์ƒ๊ด€x
  1. BFS
  • visited ์‚ฌ์šฉ to ๋ˆ„์  ๊ฑฐ๋ฆฌ

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

from collections import deque
def get_outlier(x1,y1,x2,y2):

    pos_outliers = list()
    # ๊ฐ€๋กœ 
    for x in range(x1,x2+1,1):
        pos_outliers.append([x,y1])
        pos_outliers.append([x,y2])
    # ์„ธ๋กœ
    for y in range(y1, y2+1, 1):
        if  [x1,y] in pos_outliers or [x2,y] in pos_outliers:
            continue
        pos_outliers.append([x1,y])
        pos_outliers.append([x2,y])
    return sorted(pos_outliers)
        
        
def solution(rectangle, characterX, characterY, itemX, itemY):
    answer = 0
    field = [[0 for _ in range(51*2)] for k in range(51*2)]
    #์ธ์ ‘ ํ–‰๋ ฌ
    characterX, characterY = characterX*2 , characterY*2
    itemX, itemY = itemX*2 , itemY*2
    #1.๊ฐ ์ง์‚ฌ๊ฐํ˜• ์นธ ํš๋“
    for k in range(len(rectangle)):
        #outlier ์นธ ์ฐพ๊ธฐ
        x1,y1 ,x2 ,y2 = rectangle[k]
        x1 = x1*2 ; x2 = x2*2 ; y1 = y1*2 ; y2 = y2*2 # 2๋ฐฐ 
        outliers = get_outlier(x1,y1,x2,y2)
        
        #ํ•„ํ„ฐ๋ง
        # ๋ชจ๋“  ๋ณ€ = 1  - ์˜์—ญ ๊ฒน์น˜๋Š” ๋ชจ์„œ๋ฆฌ๋งŒ =0
        for i in range(len(outliers)):
            out_x , out_y= outliers[i]
            field[out_x][out_y] = 1 
        # ์˜์—ญ ๊ฒน์น˜๋Š” ๋ชจ์„œ๋ฆฌ ํƒ์ƒ‰ -> =0 
        for j in range(len(rectangle)) : 
            if j == k : # ๋ณธ์ธ๊ป€ ์Šคํ‚ต
                continue
            fil_x1 , fil_y1 , fil_x2 , fil_y2 =  rectangle[j]
            fil_x1 , fil_y1  =fil_x1*2 , fil_y1*2
            fil_x2 , fil_y2  = fil_x2*2 , fil_y2*2 
            for i in range(len(outliers)):
                out_x , out_y = outliers[i]
                # ๋ฉด์ ์ด ๊ฒน์นจ -> ํ•„ํ„ฐ๋ง
                if fil_x2 > out_x > fil_x1 and fil_y2 > out_y > fil_y1: 
                    field[out_x][out_y] = 0 
        
        # for i in range(51):
        #     for k in range(51):
        #         if field[i][k] == 1:
        #             print (f"[{i},{k}")

    # stage 2. BFS๋กœ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ฐพ๊ธฐ
    # ์ƒํ•˜์ขŒ์šฐ
    dx =[0,0,1,-1]
    dy= [1,-1,0,0]
    queue = deque([(characterX,characterY)]) #queue ์ดˆ๊ธฐํ™”
    field[characterX][characterY]=0
    while queue : 
        cur_x, cur_y = queue.popleft()
        #์ธ์ ‘ ๋…ธ๋“œ
        for idx in range(4):
            # ๋ฃจํŠธ๋งŒ ๊ฐ€๋Šฅ
            next_x ,next_y = cur_x + dx[idx] , cur_y + dy[idx]
            if field[next_x][next_y] == 1   : # ๋ฐฉ๋ฌธ x & ๊ธธ
                field[next_x][next_y] = field[cur_x][cur_y] +1 
                # print(f"#{field[next_x][next_y]}")
                if next_x == itemX and next_y == itemY: # ๋ชฉ์ ์ง€ ๋„์ฐฉ -> ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
                    # print(f"#####{field[next_x][next_y]}")
                    return field[next_x][next_y]//2
                    break
                queue.append((next_x,next_y))
                
        # print(f"#: {cur_x},{cur_y} => {field[cur_x][cur_y]}")
        
        # print(f">{queue}")
        # print(f"##{field[next_x][next_y]}")
    
    
    return answer

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค . ๋‹จ์–ด๋ณ€ํ™˜: DFS&BFS / level3

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

# ์ตœ์†Œ ํŽธ์ง‘ ๊ฑฐ๋ฆฌ =1 ํ™•์ธ
def check_edit1(a,b):
    diff= 0 
    for i in range(len(a)) :
        if a[i] != b[i] :
            diff+=1
        if diff > 1 : 
            return False
    if diff == 1 : 
        return True
        
from collections import deque
def solution(begin, target, words):
    answer = 0
    #excep1 :words ์— target์—†์œผ๋ฉด -> ๋ณ€ํ™˜ ๋ถˆ๊ฐ€
    if target not in words :
        return 0 
    
    # 1. ์ธ์ ‘ graph ์ƒ์„ฑ
    graph = [[] for _ in range(len(words)+2)] 
    # begin -> words, target ๋‹จ๋ฐฉํ–ฅ 
    for k in range(len(words)) :
        if check_edit1(begin, words[k]) :
            graph[0].append(k+1)
    if check_edit1(begin,target):
        graph[0].append(len(words)+1)
    # words -> wrods_1(์Œ๋ฐฉํ–ฅ) , target(๋‹จ๋ฐฉํ–ฅ)    
    for k in range(len(words)):
        for j in range(k ,len(words)):
            if check_edit1(words[k], words[j]):
                graph[k+1].append(j+1)
                graph[j+1].append(k+1)
        if check_edit1(words[k], target):
            graph[k+1].append(len(words)+1)

    #2.BFS 
    visited = [ 0 for _ in range(len(words)+2)]
    def bfs(visited):
        visited[0] = 0
        queue = deque()
        queue.append(0)

        while queue :
            curr_idx = queue.popleft()
            if len(graph[curr_idx]) <= 0 : # ์ด์›ƒํ•œ ๋†ˆ์ด ์—†๋Š” ๊ฒฝ์šฐ => return 0 
                return 0

            for next_idx in graph[curr_idx] : 
                if not visited[next_idx] :  

                    queue.append(next_idx)
                    visited[next_idx] = visited[curr_idx] + 1 

                if visited[-1] : #์„ฑ๊ณต
                    return visited[-1]
        
        return visited[-1] 
    

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค . ๊ฒŒ์ž„๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ: DFS&BFS / level2

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

from collections import deque
def solution(maps):
    n,m = len(maps) ,len(maps[0])# y๊ธธ์ด # x ๊ธธ์ด 
    
    # ์ด๋™ ๋ฒ”์œ„ - ํ•˜,์šฐ,์ƒ,์ขŒ
    dx = [0,1 , 0,-1]
    dy = [1 , 0 , -1 , 0]
    queue = deque([(0,0)])
    visited = [[False for _ in range(m)] for k in range(n)]
    visited[0][0] = True
    #2. BFS
    while queue :
        # ํ˜„์œ„์น˜ 
        curr_y , curr_x = queue.popleft()
        
        # ํ†ต๋กœ ํ™•์ธ - ๋ฒฝor ๋ฐ”์šด๋”๋ฆฌ ๋ฐ–์ด๋ฉด x 
        for i in range(4):
            next_y = curr_y + dy[i]
            next_x = curr_x + dx[i]
            # target ๋„์ฐฉ ํ™•์ธ 
            if next_y == n-1 and next_x == m-1 and maps[next_y][next_x] == 1:
                visited[next_y][next_x] = True
                maps[next_y][next_x] = maps[curr_y][curr_x] +1
                break
            # ๋ฐ”์šด๋”๋ฆฌ ์•ˆ์— ์žˆ์Œ * ๊ธธ์ธ ๊ฒฝ์šฐ + ๋ฐฉ๋ฌธ ๋“ฑ๋ก x 
            if 0<=next_y< n and 0<= next_x < m :
                if maps[next_y][next_x] == 1 and not visited[next_y][next_x]:
                    maps[next_y][next_x] = maps[curr_y][curr_x] +1 
                    queue.append((next_y, next_x))
                    visited[next_y][next_x] = True

          
    # 3. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ถœ๋ ฅ
    # print(answer_list)
    if not visited[n-1][m-1]:
        return -1 
    # answer = min(answer_list)
    answer= maps[-1][-1]
    # print(answer)
    return answer

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค . ์™„์ „๋ฒ”์ฃ„: DFS&BFS / level2

์ •๋ฆฌํ•œ ๋งํฌ: (๋ฐ”๋กœ๊ฐ€๊ธฐ)

๐Ÿšฉ์ œ์ถœํ•œ ์ฝ”๋“œ

def solution(info, n, m):
    answer = 0
    #1. dp ํ…Œ์ด๋ธ” ์ •์˜
    dp = [[] for _ in range(len(info))]

    if info[0][0] < n: 
        dp[0].append((info[0][0]  , 0))
    if info[0][1]  < m :
        dp[0].append((0, info[0][1]))
    if len(dp[0]) <= 0 :
        return -1
    # print(f" # item 0 : {dp[0]}")
    for i in range(1, len(info)):
        # dp[i][k]
        for cur_a ,cur_b in dp[i-1]:
            # print(f"##cur_a ,cur_b  {cur_a},{cur_b }")
            up_a = cur_a + info[i][0]
            up_b = cur_b + info[i][1]
            if up_a < n: 
                dp[i].append((up_a , cur_b))
            if up_b < m :
                dp[i].append((cur_a , up_b))
            # print(f" # item {i} : {dp[i]}")
        if len(dp[i]) <= 0 :
            return -1
        # print(f" # item {i} : {dp[i]}")
    # A ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜ 
    answer = sorted(dp[-1])[0][0]
    return answer

@github-actions
Copy link

github-actions bot commented Apr 8, 2025

2025-04 ์ฑŒ๋ฆฐ์ง€ ์ง„ํ–‰ ์ƒํ™ฉ

์‚ฌ์šฉ์ž ์ฑŒ๋ฆฐ์ง€ ์œ ํ˜• ๋ฌธ์ œ ์ˆ˜ ๋‹ฌ์„ฑ ์—ฌ๋ถ€
Mingguriguri ๊ทธ๋ž˜ํ”„ 0 โŒ
Mingguriguri DP 5 โœ…
zaqquum ๊ทธ๋ž˜ํ”„ 0 โŒ
zaqquum DP 2 โŒ

@Mingguriguri Mingguriguri reopened this Apr 8, 2025
@github-actions
Copy link

github-actions bot commented Apr 8, 2025

2025-04 ์ฑŒ๋ฆฐ์ง€ ์ง„ํ–‰ ์ƒํ™ฉ

์‚ฌ์šฉ์ž ์ฑŒ๋ฆฐ์ง€ ์œ ํ˜• ๋ฌธ์ œ ์ˆ˜ ๋‹ฌ์„ฑ ์—ฌ๋ถ€
Mingguriguri ๊ทธ๋ž˜ํ”„ 0 โŒ
Mingguriguri DP 5 โœ…
zaqquum ๊ทธ๋ž˜ํ”„ 0 โŒ
zaqquum DP 2 โŒ

@Mingguriguri Mingguriguri reopened this Apr 8, 2025
@Mingguriguri Mingguriguri merged commit 350d728 into main Apr 8, 2025
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants