DevYoon

[SWEA] 2105. ๋””์ €ํŠธ ์นดํŽ˜ (Python) ๋ณธ๋ฌธ

PS/SWEA

[SWEA] 2105. ๋””์ €ํŠธ ์นดํŽ˜ (Python)

gimewn 2022. 4. 7. 16:45

link ๐Ÿ”— https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5VwAr6APYDFAWu 

 

SW Expert Academy

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

swexpertacademy.com

 

def dfs(num, cy, cx, visit):
    global Max, sy, sx
    if num > 3:
        return
    if num == 3 and cy == sy and cx == sx:
        if Max < len(visit):
            Max = len(visit)
            return

    for idx in range(num, num+2): # ํ˜„์žฌ ๋ฐฉํ–ฅ, ํ˜„์žฌ๋ฐฉํ–ฅ+1
        dy, dx = cy+directy[idx], cx+directx[idx]
        if 0 <= dy < n and 0 <= dx < n:
            if arr[dy][dx] not in visit:
                visit.append(arr[dy][dx])
                dfs(idx, dy, dx, visit)
                visit.pop()

t = int(input())

for tc in range(1, t+1):
    n = int(input())
    arr = [list(map(int, input().split())) for _ in range(n)]
    Max = -1
    directy = [1, 1, -1, -1, 100] # 4๋ฒˆ์งธ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ ์•„๋ฌด ์ˆซ์ž๋‚˜
    directx = [-1, 1, 1, -1, 100]
    for y in range(n-2):
        for x in range(1, n-1): # 0์—ด๊ณผ n-1์—ด์€ ์‚ฌ๊ฐํ˜• ์ƒ์„ฑ ๋ถˆ๊ฐ€๋Šฅ
            sy, sx = y, x # ์‹œ์ž‘์  ๋ณ€์ˆ˜ ๋งŒ๋“ค์–ด ์ฃผ๊ธฐ
            dfs(0, sy, sx, []) # ๋ฐฉํ–ฅ, y์ขŒํ‘œ, x์ขŒํ‘œ, visit
    print(f'#{tc} {Max}')

 

๐Ÿ’ฅ ์ฃผ์˜

  • ํ•˜............^^
  • DFS๋ฅผ ์ด๋ ‡๊ฒŒ๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ตฌ๋‚˜ ๋˜ ๋ฐฐ์› ๋‹คใ… ใ… 
  • ๊ผญ ํ•œ ๋ฒˆ ๋” ํ’€์–ด๋ณผ ๊ฒƒ