๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV2 ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡

๐ŸŽฎinspirer9 2023. 7. 31. 01:15
728x90
๋ฐ˜์‘ํ˜•

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต ๋ฌธ์ œ ๋ ˆ๋ฒจ2์— 4๋ฌธ์ œ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค.

๊ทธ์ค‘ ์ •๋‹ต๋ฅ ์ด ์ œ์ผ ๋†’์€ (38%) ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡๋ถ€ํ„ฐ ํ’€์–ด๋ณด๊ธฐ๋กœ ํ•˜์ž!

๊ทผ๋ฐ ์•„๋ฌด๋ฆฌ ๋ด๋„ ๋‚ด ๋Œ๋จธ๋ฆฌ๋กœ๋Š” ใ…‹ใ…‹ใ…‹ ๋„์ €ํžˆ ํ’€๋ฆฌ์ง€ ์•Š๋Š” ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡... ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡์€ ๋ณด๋“œ๊ฒŒ์ž„ ์ด๋ฆ„์ธ๋ฐ, ๊ทธ ๋ณด๋“œ๊ฒŒ์ž„์˜ ๋กœ๋ด‡ ์›€์ง์ž„๊ณผ ๋น„์Šทํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๋ฌธ์ œ๋Š” ๋ณด๋“œ๊ฒŒ์ž„์ฒ˜๋Ÿผ ์‰ฝ์ง€๊ฐ€ ์•Š์•˜๋‹ค.

๋ฌธ์ œ ์„ค๋ช…

๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡์ด๋ผ๋Š” ๋ณด๋“œ๊ฒŒ์ž„์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์ด ๋ณด๋“œ๊ฒŒ์ž„์€ ๊ฒฉ์ž๋ชจ์–‘ ๊ฒŒ์ž„ํŒ ์œ„์—์„œ ๋ง์„ ์›€์ง์ด๋Š” ๊ฒŒ์ž„์œผ๋กœ, ์‹œ์ž‘ ์œ„์น˜์—์„œ ๋ชฉํ‘œ ์œ„์น˜๊นŒ์ง€ ์ตœ์†Œ ๋ช‡ ๋ฒˆ๋งŒ์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋งํ•˜๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค.
์ด ๊ฒŒ์ž„์—์„œ ๋ง์˜ ์›€์ง์ž„์€ ์ƒ, ํ•˜, ์ขŒ, ์šฐ 4๋ฐฉํ–ฅ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์„œ ๊ฒŒ์ž„ํŒ ์œ„์˜ ์žฅ์• ๋ฌผ์ด๋‚˜ ๋งจ ๋์— ๋ถ€๋”ชํž ๋•Œ๊นŒ์ง€ ๋ฏธ๋„๋Ÿฌ์ ธ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ํ•œ ๋ฒˆ์˜ ์ด๋™์œผ๋กœ ์นฉ๋‹ˆ๋‹ค.
๋‹ค์Œ์€ ๋ณด๋“œ๊ฒŒ์ž„ํŒ์„ ๋‚˜ํƒ€๋‚ธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

. . . D . . R
. D . G . . .
. . . . D . D
D . . . . D .
. . D . . . .

์—ฌ๊ธฐ์„œ "."์€ ๋นˆ ๊ณต๊ฐ„์„, "R"์€ ๋กœ๋ด‡์˜ ์ฒ˜์Œ ์œ„์น˜๋ฅผ, "D"๋Š” ์žฅ์• ๋ฌผ์˜ ์œ„์น˜๋ฅผ, "G"๋Š” ๋ชฉํ‘œ์ง€์ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
์œ„ ์˜ˆ์‹œ์—์„œ๋Š” "R" ์œ„์น˜์—์„œ ์•„๋ž˜, ์™ผ์ชฝ, ์œ„, ์™ผ์ชฝ, ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ, ์œ„ ์ˆœ์„œ๋กœ ์›€์ง์ด๋ฉด 7๋ฒˆ ๋งŒ์— "G" ์œ„์น˜์— ๋ฉˆ์ถฐ ์„ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๊ฒƒ์ด ์ตœ์†Œ ์›€์ง์ž„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
๊ฒŒ์ž„ํŒ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด board๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ง์ด ๋ชฉํ‘œ์œ„์น˜์— ๋„๋‹ฌํ•˜๋Š”๋ฐ ์ตœ์†Œ ๋ช‡ ๋ฒˆ ์ด๋™ํ•ด์•ผ ํ•˜๋Š”์ง€ return ํ•˜๋Š” solutionํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ๋งŒ์•ฝ ๋ชฉํ‘œ์œ„์น˜์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ return ํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ board์˜ ๊ธธ์ด ≤ 100
    • 3 ≤ board์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 100
    • board์˜ ์›์†Œ์˜ ๊ธธ์ด๋Š” ๋ชจ๋‘ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฌธ์ž์—ด์€ ".", "D", "R", "G"๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ ๋นˆ ๊ณต๊ฐ„, ์žฅ์• ๋ฌผ, ๋กœ๋ด‡์˜ ์ฒ˜์Œ ์œ„์น˜, ๋ชฉํ‘œ ์ง€์ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • "R"๊ณผ "G"๋Š” ํ•œ ๋ฒˆ์”ฉ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

board result
["...D..R", ".D.G...", "....D.D", "D....D.", "..D...."] 7
[".D.R", "....", ".G..", "...D"] -1

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1
    • ๋ฌธ์ œ ์„ค๋ช…์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2
    • "R" ์œ„์น˜์— ์žˆ๋Š” ๋ง์„ ์–ด๋–ป๊ฒŒ ์›€์ง์—ฌ๋„ "G" ์— ๋„๋‹ฌ์‹œํ‚ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
      ๋”ฐ๋ผ์„œ -1์„ return ํ•ฉ๋‹ˆ๋‹ค.
. D . R
. . . .
. G . .
. . . D

ํ’€์ด

๋ฌด์ง€์„ฑ ํ’€์ด - ์‹คํŒจ

DFS - ์‹คํŒจ

BFS - ๋ฌดํ•œ๋ฃจํ”„ ใ…‹ใ…‹ใ…‹

๋ญ˜ ์ž˜๋ชปํ–ˆ๊ธธ๋ž˜?

๊ทธ๋ž˜์„œ ๋‹ค์‹œ ์ƒˆ๋กœ ํ’€์–ด๋ณธ๋‹ค.

. . . D . . R
. D . G . . .
. . . . D . D
D . . . . D .
. . D . . . .

์šฐ์„  ํ…Œ๋‘๋ฆฌ๋ฅผ D๋กœ ๊ฐ์‹ผ๋‹ค.

D D D D D D D D D
D . . . D . . R D
D . D . G . . . D
D . . . . D . D D
D D . . . . D . D
D . . D . . . . D
D D D D D D D D D

๋‹ต์ด ๋‚˜์˜ค์ง€ ์•Š๋Š” 3๊ฐ€์ง€ ์ผ€์ด์Šค

  • R์˜ ์‚ฌ๋ฐฉ์ด D๋กœ ๊ฐ์‹ธ์ ธ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡์ด ๋‚˜๊ฐˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์‹คํŒจ.
  • G์ฃผ๋ณ€ ์‚ฌ๋ฐฉ์ด D๋กœ ๊ฐ์‹ธ์ ธ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡์ด ๋„์ฐฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์‹คํŒจ.
  • G์ฃผ๋ณ€์— D๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡์ด ๋ฉˆ์ถœ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์‹คํŒจ.

๋‹ค์Œ์€ ์œ„ 3๊ฐ€์ง€ ์ผ€์ด์Šค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๊ธธ์„ ์ฐพ์•„์•ผ ํ•˜๋Š”๋ฐ...

  • R ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•...
    • ์ด ๊ฒฝ์šฐ๋ผ๋ฉด R ์œ„์น˜์—์„œ ์‚ฌ๋ฐฉ์œผ๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ ,
      (์ด๋™์‹œ ํ•œ์นธ์”ฉ์ด ์•„๋‹ˆ๋ผ ๋ง‰ํž ๋•Œ ๊นŒ์ง€ ์ญˆ์šฑ ๊ฐ€์•ผํ•จ)
  • G๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•...
    • ๊ฒฝ๋กœ ์ƒ์— D๊ฐ€ ์žˆ๋Š” ์œ„์น˜์—์„œ ๋ถ„๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
      (๊ทผ๋ฐ ์ด๊ฑด ์ข€ ํ—ท๊ฐˆ๋ฆผ ์ž˜ ๋ ๊นŒ?)

๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์„ ํƒํ•œ ๋ฐฉ๋ฒ•์€, ์ฒ˜์Œ ์˜ˆ์™ธ ์ผ€์ด์Šค 3๊ฐœ ์ฒ˜๋ฆฌํ•˜๊ณ , R๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ BFS๋กœ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•.

  • ๋กœ๋ด‡์˜ ์ตœ์ดˆ ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ณ , "R"์€ "."๋กœ ๋ณ€๊ฒฝ

 

๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์ง€๋งŒ ์‚ฝ์งˆ์„ ํ•œ๋‹ค. ใ…‹ใ…‹ใ…‹

from collections import deque

def solution(board):
    width = len(board[0]) + 2 # ๊ฐ€๋กœ
    height = len(board) + 2 # ์„ธ๋กœ
    board1 = []   # ์ง€๋„
    rx, ry = 0, 0 # ๋กœ๋ด‡์œ„์น˜
    dirs = ((-1, 0), (1, 0), (0, -1), (0, 1)) # BFS ํƒ์ƒ‰ 4 ๋ฐฉํ–ฅ
    visited = [[0] * width for _ in range(height)] # ๋ฐฉ๋ฌธ ์ •๋ณด
        
    board1.append(["D"] * width)
    for i,b in enumerate(board):
        _tmp = ["D"]
        for j,bb in enumerate(b):
            if bb == "R":
                rx, ry = j + 1, i + 1
            _tmp.append(bb)
        _tmp.append("D")
        board1.append(_tmp)
    board1.append(["D"] * width)
    
    visited[ry][rx] = 1
    
    def bfs():
        q = deque()
        q.append((rx, ry))
        
        while q:
            print("ํ:",q)
            px, py = q.popleft()
            
            if board1[py][px] == "G":
                return visited[py][px]
            
            for i in range(4):
                nx, ny = px, py
                while board1[ny + dirs[i][0]][nx+ dirs[i][1]] != "D":
                    nx = nx + dirs[i][1]
                    ny = ny + dirs[i][0]
                print("i:",i,"x,y:",nx,ny,"visit:",board1[ny][nx])
                for z in range(height):
                    for w in range(width):
                        if z==ny and w == nx:
                            print("R",end="")
                        else:
                            print(board1[z][w],end="")
                    print("")
                if board1[ny][nx] !="D":
                    if visited[ny][nx] == 0:
                        visited[ny][nx] = visited[py][px] + 1
                        q.append((nx, ny))

        return -1
    
    answer = bfs()
    if answer > 0: answer -= 1
    
    return answer

์‚ฝ์งˆํ•œ ๋ถ€๋ถ„์€ ๋กœ๋ด‡์ด ์ด๋™ํ•ด์„œ ๋ฒฝ์— ๋ถ€๋”ซํž ๋•Œ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒƒ, ๊ทธ๋ฆฌ๊ณ  ์œ„์น˜ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„. print๋กœ ๊ณ„์† ์ฐ์–ด๊ฐ€๋ฉด์„œ ํ•˜๋‹ค๊ฐ€  ๊ฒฐ๊ตญ ํžŒํŠธ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

from collections import deque

def solution(board):
    width = len(board[0]) + 2 # ๊ฐ€๋กœ
    board1 = []   # ์ง€๋„
    rx, ry = 0, 0 # ๋กœ๋ด‡์œ„์น˜
    dirs = ((-1, 0), (1, 0), (0, -1), (0, 1)) # BFS ํƒ์ƒ‰ 4 ๋ฐฉํ–ฅ
    visited = [[0] * width for _ in range(len(board) + 2)] # ๋ฐฉ๋ฌธ ์ •๋ณด
    q = deque() # BFS ํƒ์ƒ‰ ๋‹ค์Œ ๋กœ๋ด‡ ์œ„์น˜
    
    board1.append(["D"] * width)
    for i,b in enumerate(board):
        _tmp = ["D"]
        for j,bb in enumerate(b):
            if bb == "R":
                rx, ry = j + 1, i + 1
            _tmp.append(bb)
        _tmp.append("D")
        board1.append(_tmp)
    board1.append(["D"] * width)
    
    visited[ry][rx] = 1
    
    def bfs():
        q.append((rx, ry))
        
        while q:
            px, py = q.popleft()
            
            if board1[py][px] == "G":
                return visited[py][px]
            
            for i in range(4):
                nx, ny = px, py
                while board1[ny + dirs[i][0]][nx+ dirs[i][1]] != "D":
                    nx = nx + dirs[i][1]
                    ny = ny + dirs[i][0]
                if board1[ny][nx] !="D" and visited[ny][nx] == 0:
                        visited[ny][nx] = visited[py][px] + 1
                        q.append((nx, ny))
        return -1
    
    answer = bfs()
    
    if answer > 0:
        return answer - 1
    else:
        return answer
        
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (5.89ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (4.85ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.79ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.61ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (7.79ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.90ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (4.52ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.56ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (2.88ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.64ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.75ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (2.74ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (7.22ms, 10.3MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (7.26ms, 10.4MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (2.95ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (2.85ms, 10.4MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.86ms, 10.2MB)

ํ ... ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด...

์ฝ”๋“œ๊ฐ€ ์ฐธ ์งง๊ตฌ๋‚˜...;;;

def solution(board):
    que = []
    for x, row in enumerate(board):
        for y, each in enumerate(row):
            if board[x][y] == 'R':
                que.append((x, y, 0))
    visited = set()
    while que:
        x, y, length = que.pop(0)
        if (x, y) in visited:
            continue
        if board[x][y] == 'G':
            return length
        visited.add((x, y))
        for diff_x, diff_y in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            now_x, now_y = x, y
            while True:
                next_x, next_y = now_x + diff_x, now_y + diff_y
                if 0 <= next_x < len(board) and 0 <= next_y < len(board[0]) and board[next_x][next_y] != 'D':
                    now_x, now_y = next_x, next_y
                    continue
                que.append((now_x, now_y, length + 1))
                break
    return -1
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (15.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (5.51ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (2.24ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.90ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.39ms, 9.93MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (10.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.94ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (6.26ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (5.15ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.05ms, 10MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.1MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.35ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (3.28ms, 10.1MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.67ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.85ms, 10.1MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (2.96ms, 10.1MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (9.26ms, 10.1MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.26ms, 10.1MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (9.64ms, 10.1MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (3.64ms, 10.3MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (3.36ms, 10.1MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (1.12ms, 10MB)

deque๋ฅผ ์“ฐ๊ณ  ์•ˆ์“ฐ๊ณ ์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ํŽธ... ๊ทธ ์™ธ์—๋Š” BFS๋ผ์„œ ๋Œ€๋ถ€๋ถ„ ์ฝ”๋“œ๊ฐ€ ๋น„์Šทํ•˜๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—” ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋…์„ฑ ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

from collections import deque

def solution(board):
    answer = -1
    direction = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    q = deque()
    n, m = len(board), len(board[0])
    visited = [[False]*m for _ in range(n)]

    for x in range(n):
        for y in range(m):
            if board[x][y] == "R":
                sx, sy = x, y

    q.append((sx, sy, 0))

    while q:
        x, y, level = q.popleft()

        if board[x][y] == "G":
            answer = level
            break

        for dx, dy in direction:
            scope = 1
            while 1:
                nx, ny = x + dx*scope, y + dy*scope
                if nx < 0 or nx >= n or ny < 0 or ny >= m or board[nx][ny] == "D":
                    if visited[nx-dx][ny-dy] == False:
                        visited[nx-dx][ny-dy] = True
                        q.append((nx-dx, ny-dy, level+1))

                    break
                scope += 1

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (5.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (4.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.62ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.69ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (6.55ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.76ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (3.99ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (2.53ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.51ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.66ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (2.26ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (6.79ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.98ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.37ms, 9.98MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (8.92ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (2.98ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (2.47ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.83ms, 10.2MB)
728x90
๋ฐ˜์‘ํ˜•