🧱 λ²½λΆ€μˆ˜κ³  μ΄λ™ν•˜κΈ°

  • μ‹œκ°„μ œν•œ: 2초
  • λ©”λͺ¨λ¦¬ μ œν•œ: 192MB

πŸ“„ 문제

  • N x M의 ν–‰λ ¬λ‘œ ν‘œν˜„λ˜λŠ” 맡이 μžˆλ‹€. 
  • λ§΅μ—μ„œλŠ” 0은 이동할 수 μžˆλŠ” 곳을 λ‚˜νƒ€λ‚΄κ³ , 1은 이동할 수 μ—†λŠ” 벽이 μžˆλŠ” 곳을 λ‚˜νƒ€λ‚Έλ‹€.
  • 당신은 (1,1) μ—μ„œ (N, M)의 μœ„μΉ˜κΉŒμ§€ μ΄λ™ν•˜λ € ν•˜λŠ”λ°, μ΄λ•Œ μ΅œλ‹¨ 경둜둜 μ΄λ™ν•˜λ € ν•œλ‹€.
  • μ΅œλ‹¨κ²½λ‘œλŠ” λ§΅μ—μ„œ κ°€μž₯ 적은 개수의 칸을 μ§€λ‚˜λŠ” 경둜λ₯Ό λ§ν•˜λŠ”λ°, μ΄λ•Œ μ‹œμž‘ν•˜λŠ” μΉΈκ³Ό λλ‚˜λŠ” 칸도 ν¬ν•¨ν•΄μ„œ μ„Όλ‹€.
  • λ§Œμ•½μ— μ΄λ™ν•˜λŠ” 도쀑에 ν•œ 개의 벽을 λΆ€μˆ˜κ³  μ΄λ™ν•˜λŠ” 것이 μ’€ 더 κ²½λ‘œκ°€ 짧아진닀면, 벽을 ν•œ 개 κΉŒμ§€ λΆ€μˆ˜κ³  μ΄λ™ν•˜μ—¬λ„ λœλ‹€.
  • ν•œ μΉΈμ—μ„œ 이동할 수 μžˆλŠ” 칸은 μƒν•˜μ’Œμš°λ‘œ μΈμ ‘ν•œ 칸이닀.
  • 맡이 μ£Όμ–΄μ‘Œμ„ λ•Œ, μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•΄λ‚΄λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μž…λ ₯

  • 첫 쀄에 N(1 <= N <= 1000), M(1 <= M <= 1000)이 주어진닀.
  • λ‹€μŒ N개의 쀄에  M개의 숫자둜 맡이 주어진닀.
  • (1,1)κ³Ό (N, M)은 항상 0이라고 κ°€μ •ν•˜μž.

좜λ ₯

  • 첫째 쀄에 μ΅œλ‹¨ 거리λ₯Ό 좜λ ₯ν•œλ‹€. λΆˆκ°€λŠ₯ν•  λ•ŒλŠ” -1을 좜λ ₯ν•œλ‹€.

πŸ’‘ 문제 μ ‘κ·Ό 아이디어

  • (1, 1)κ³Ό (N, M) 각각 ν•˜, μš°μ™€ 상, μ’Œκ°€ 1인 경우 == -1
  • κ°€λ‹€κ°€ 길이 λŠκΈ°λŠ” 경우 == -1
  • DFS μ•Œκ³ λ¦¬μ¦˜

 

❌ 2% ν‹€λ ΈμŠ΅λ‹ˆλ‹€.

  • λ„ˆλ¬΄ λ‹¨μˆœν•˜κ²Œ 예제 μ½”λ“œμ—λ§Œ λ§žμΆ°μ„œ μƒκ°ν•œ 것 κ°™λ‹€.
  • λ‹€μ‹œ μƒκ°ν•΄λ³΄μž.
  • DFSκ°€ μ•„λ‹ˆλΌ BFS둜 ν’€μ–΄μ•Ό ν•˜λŠ”κ²Œ λ§žλŠ” 것 κ°™μŒ! 
from sys import stdin

input = stdin.readline

N, M = map(int, input().split())
walls = []

for _ in range(N):
    walls.append(list(map(int, input().strip())))

# (1,1)κ³Ό (N,M)이 1둜 κ°€λ‘œλ§‰ν˜€ μžˆλŠ”μ§€ 확인
start = walls[0][1] and walls[1][0]
end = walls[N-1][M-2] and walls[N-2][M-1]

if start and end:
    print(-1)
    exit()


def dfs(graph, start):
    # start (x, y, value, walls-flag)
    stack = [start]

    # directions
    directionsX = [1, -1, 0, 0]
    directionsY = [0, 0, -1, 1]

    while stack:
        x, y, value, flag = stack.pop()
        # print(x, y, value, flag)
        # for g in graph:
        #     print(g)
        # print()

        graph[x][y] = min(value, graph[x][y]) if graph[x][y] > 1 else value

        for i in range(len(directionsX)):
            nx, ny = x + directionsX[i], y + directionsY[i]
            if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] in [0, 1]:
                if flag:
                    if graph[nx][ny] == 0:
                        stack.append((nx, ny, value + 1, flag))
                else:
                    if graph[nx][ny] == 1:
                        stack.append((nx, ny, value + 1, True))
                    else:
                        stack.append((nx, ny, value + 1, flag))
    return graph

result = dfs(walls, (0, 0, 2, False))

if result[N-1][M-1] > 1:
    print(result[N-1][M-1] - 1)
else:
    print(-1)

 

❌ λ©”λͺ¨λ¦¬ 초과 >> 이런,,

  • visited checkν• λ•Œ set μ‚¬μš©, 4 tuple μ‚¬μš© λ“± 이슈둜 λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λ‚œ 것 κ°™λ‹€λŠ” μ˜ˆμƒ >> 3차원 λ°°μ—΄λ‘œ visited μ„ μ–Έν•΄μ„œ λ°©λ¬Έ 체크둜 λ³€κ²½
from sys import stdin
from collections import deque

input = stdin.readline

N, M = map(int, input().split())
walls = []

for _ in range(N):
    walls.append(list(map(int, input().strip())))

# (1,1)κ³Ό (N,M)이 1둜 κ°€λ‘œλ§‰ν˜€ μžˆλŠ”μ§€ 확인
start = walls[0][1] and walls[1][0]
end = walls[N-1][M-2] and walls[N-2][M-1]

if start and end:
    print(-1)
    exit()


def bfs(graph, start):
    # start (x, y, value, walls-flag)
    queue = deque([start])

    # directions
    directionsX = [-1, 1, 0, 0]
    directionsY = [0, 0, 1, -1]

    while queue:
        x, y, value, flag = queue.popleft()
        # print(x, y, value, flag)
        # for g in graph:
        #     print(g)
        # print(graph[x][y], value)
        # print()

        graph[x][y] = min(value, graph[x][y]) if graph[x][y] > 1 else value

        for i in range(len(directionsX)):
            nx, ny = x + directionsX[i], y + directionsY[i]
            if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] in [0, 1]:
                if flag:
                    if graph[nx][ny] == 0:
                        queue.append((nx, ny, value + 1, flag))
                else:
                    if graph[nx][ny] == 1:
                        queue.append((nx, ny, value + 1, True))
                    else:
                        queue.append((nx, ny, value + 1, flag))
    return graph

result = bfs(walls, (0, 0, 2, False))

if result[N-1][M-1] > 1:
    print(result[N-1][M-1] - 1)
else:
    print(-1)


# 7 4
# 0000
# 1110
# 1000
# 1000
# 0000
# 0111
# 0000
# answer: 10

 

βœ…  성곡 μ½”λ“œ 

from sys import stdin
from collections import deque

input = stdin.readline

N, M = map(int, input().split())
walls = []

for _ in range(N):
    walls.append(list(map(int, input().strip())))

def bfs(graph, start):
    queue = deque([start])
    visited = [[[0, 0] for _ in range(M)] for _ in range(N)]
    visited[0][0][0] = 1 # μ‹œμž‘μ  λ°©λ¬Έ ν‘œμ‹œ

    # directions
    directions = [(-1, 0), (1, 0), (0, 1), (0, -1)]

    while queue:
        x, y, flag = queue.popleft()

        if x == N - 1 and y == M - 1:
            return visited[x][y][flag]

        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            if 0 <= nx < N and 0 <= ny < M:
                # 벽이 μ•„λ‹ˆκ³  아직 λ°©λ¬Έν•˜μ§€ μ•Šμ€ 경우
                if graph[nx][ny] == 0 and visited[nx][ny][flag] == 0:
                    visited[nx][ny][flag] = visited[x][y][flag] + 1
                    queue.append((nx, ny, flag))
                # 벽이고 아직 벽을 λΆ€μˆ˜μ§€ μ•Šμ€ 경우
                elif graph[nx][ny] == 1 and flag == 0 and visited[nx][ny][1] == 0:
                    visited[nx][ny][1] = visited[x][y][0] + 1
                    queue.append((nx, ny, 1))
    # 도달할 수 없을 경우
    return -1

print(bfs(walls, (0, 0, 0)))


# 7 4
# 0000
# 1110
# 1000
# 1000
# 0000
# 0111
# 0000
# answer: 10
728x90

+ Recent posts