[BOJ][๐ŸŸก4][๋ฐฑ์ค€#02206] ๋ฒฝ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2206


๋ฌธ์ œ

Nร—M์˜ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„๋˜๋Š” ๋งต์ด ์žˆ๋‹ค. ๋งต์—์„œ 0์€ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ด๊ณ , 1์€ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ๋ฒฝ์ด ์žˆ๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹น์‹ ์€ (1, 1)์—์„œ (N, M)์˜ ์œ„์น˜๊นŒ์ง€ ์ด๋™ํ•˜๋ ค ํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜๋ ค ํ•œ๋‹ค. ์ตœ๋‹จ๊ฒฝ๋กœ๋Š” ๋งต์—์„œ ๊ฐ€์žฅ ์ ์€ ๊ฐœ์ˆ˜์˜ ์นธ์„ ์ง€๋‚˜๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งํ•˜๋Š”๋ฐ, ์ด๋•Œ ์‹œ์ž‘ํ•˜๋Š” ์นธ๊ณผ ๋๋‚˜๋Š” ์นธ๋„ ํฌํ•จํ•ด์„œ ์„ผ๋‹ค. ๋งŒ์•ฝ์— ์ด๋™ํ•˜๋Š” ๋„์ค‘์— ํ•œ ๊ฐœ์˜ ๋ฒฝ์„ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๊ฒฝ๋กœ๊ฐ€ ์งง์•„์ง„๋‹ค๋ฉด, ๋ฒฝ์„ ํ•œ ๊ฐœ ๊นŒ์ง€ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜์—ฌ๋„ ๋œ๋‹ค. ํ•œ ์นธ์—์„œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์นธ์€ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ์นธ์ด๋‹ค. ๋งต์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด ๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 โ‰ค N โ‰ค 1,000), M(1 โ‰ค M โ‰ค 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— M๊ฐœ์˜ ์ˆซ์ž๋กœ ๋งต์ด ์ฃผ์–ด์ง„๋‹ค. (1, 1)๊ณผ (N, M)์€ ํ•ญ์ƒ 0์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋ถˆ๊ฐ€๋Šฅํ•  ๋•Œ๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

6 4
0100
1110
1000
0000
0111
0000


์ถœ๋ ฅ

15


์˜ˆ์ œ 2

์ž…๋ ฅ

4 4
0111
1111
1111
1110


์ถœ๋ ฅ

-1


My Sol : ์˜ค๋‹ต

from collections import deque

def BFS(v):
    Q = deque()
    Q.append((0, 0, 0))
    visit[0][0][0] = 1
    ql = 1
    cnt = 1
    while Q:
        for _ in range(ql):
            ni, nj, bw = Q.popleft()
            ql -= 1
            if ni==I-1 and nj==J-1: return cnt

            for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
                si, sj = ni+di, nj+dj
                if not (0<=si<I and 0<=sj<J): continue
                sw = bw + mat[si][sj]
                if sw > 1: continue
                if visit[si][sj][sw]: continue
                visit[si][sj][sw] = 1
                Q.append((si, sj, sw))
                ql += 1
        cnt += 1

    return -1

I, J = map(int, input().split())
mat = [list(map(int, input())) for _ in range(I)]
visit = [[[0]*3 for _ in range(J)] for _ in range(I)]

print(BFS(mat))

BFS๋ฅผ ์ด์šฉํ•˜๋Š”๋ฐ, 3์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ํŠน์ดํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ช‡ ์‹œ๊ฐ„์„ ๋งค๋‹ฌ๋ฆฌ๊ณ  ๋ช‡ ๋ฒˆ์„ ํ’€์–ด๋„ ํ‹€๋ ค์„œ ๊ฒฐ๊ตญ ๋‹ต์„ ์ฐธ๊ณ ํ–ˆ๊ณ , ๋ฐฉ์‹์„ ์•„๋Š”๋ฐ๋„ ๊ณ„์† ValueError๋กœ ์˜ค๋‹ต์ด ๋–ด๋‹ค. (๋‚˜์ค‘์— ์•Œ์•˜์ง€๋งŒ sys.stdin.readline ์˜ค๋ฅ˜์˜€๋‹ค.) Queue์— ์ˆ˜๋ฅผ ๋งŽ์ด ๋„ฃ๊ณ  ์‹ถ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ๋œ ํƒ์ƒ‰์ขŒํ‘œ๋ฅผ ํ™•์ธํ•ด์„œ ๋ชฉํ‘œ์ขŒํ‘œ์ธ I-1๊ณผ J-1์ด๋ผ๋ฉด cnt+1์„ ์ถœ๋ ฅํ•˜๋„๋ก ์„ค์ •ํ–ˆ๋Š”๋ฐ, ๊ณ„์† ํ‹€๋ ค์„œ ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ๋„ฃ๊ณ , ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ๋ชฉํ‘œ ์ขŒํ‘œ์ธ ๊ฒฝ์šฐ cnt๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ๋ฐ”๊ฟจ๋‹ค. ๋ฌด์Šจ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€๋Š” ์ž˜์€ ๋ชจ๋ฅด๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

from collections import deque

di, dj = ((-1, 1, 0, 0), (0, 0, -1, 1))

N, M = map(int, input().split())
arr = [list(map(int, input())) for _ in range(N)]
visited = [[[0 for _ in range(M)] for _ in range(N)] for _ in range(2)] # ๋ฒฝ์„ ๋งŒ๋‚ฌ์„ ๋•Œ์™€ ์•ˆ ๋งŒ๋‚ฌ์„ ๋•Œ์˜ 3์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ


queue = deque()
visited[0][0][0] = 1
queue.append([0, 0, 0])

if N == 1 and M == 1 and arr[0][0] == 0:
    print(1)
else:
    flag = 0
    while queue:
        x, y, z = queue.popleft()

        for i in range(4):
            ni = x + di[i]
            nj = y + dj[i]
            # ๋ฒ”์œ„ ๋‚ด์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์„ ๋•Œ
            if 0 <= ni < N and 0 <= nj < M and visited[z][ni][nj] == 0:
                # ๋ฒฝ์„ ๋งŒ๋‚ฌ์„ ๋•Œ
                if arr[ni][nj] == 1 and z == 0:
                    visited[1][ni][nj] = visited[0][x][y] + 1
                    queue.append((ni, nj, 1))

                # ๋ฒฝ์„ ๋งŒ๋‚˜์ง€ ์•Š์•˜์„ ๋•Œ
                elif arr[ni][nj] == 0:
                    visited[z][ni][nj] = visited[z][x][y] + 1
                    queue.append([ni, nj, z])

                # ๋„์ฐฉํ–ˆ์„ ๋•Œ
                if ni == N-1 and nj == M-1:
                    print(visited[z][ni][nj])
                    flag = 1
                    break
        # ๋„์ฐฉํ–ˆ์œผ๋ฉด while ๋ฌธ ์ข…๋ฃŒ
        if flag == 1:
            break

    # ๋„์ฐฉ์ง€์ ์„ ๊ฐ€์ง€ ๋ชปํ–ˆ์„ ๋•Œ
    if flag == 0:
        print(-1)

ํƒ์ƒ‰ ์‹œ๊ฐ„์ด ์ ˆ๋ฐ˜์— ๋‹ฌํ•˜๋Š” ๋‹ต์•ˆ์ด ์žˆ์–ด์„œ ๋ถ„์„ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์—ญ์‹œ 3์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ–ˆ๊ณ , ์ƒํ™ฉ์„ ๋‚˜๋ˆ„์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์…จ๋‹ค. queue์—๋Š” tuple๊ณผ list๋ฅผ ํ˜ผ์šฉํ•ด์„œ ๋„ฃ์—ˆ๋Š”๋ฐ ์–ด์ฐจํ”ผ queue์—์„œ ๋บ„ ๋•Œ unpackingํ•ด์„œ ํ™œ์šฉํ•˜๋ฏ€๋กœ ํฌ๊ฒŒ ์ƒ๊ด€์€ ์—†์œผ๋‚˜ ์‹ค์ˆ˜์ธ ๊ฒƒ ๊ฐ™๋‹ค. ํŠน์ดํ•œ ๊ฒƒ์€ visited์— ์ด์ „ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ 1์„ ๋”ํ•˜๋Š” ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์˜€๋‹ค. ๋‚˜๋Š” while๋ฌธ์„ queue ๊ธธ์ด๋งŒํผ ๋Œ๋ฆฌ๋ฉด cnt๋ฅผ 1 ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ด ๋ฐฉ๋ฒ•์ด ๋” ๊ธฐ์ดˆ์ ์ธ ๋ฐฉ์‹์ด์ง€๋งŒ ์ด๋ฒˆ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” ํ’€์ด์— ์ข‹์•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๋ฒฝ์„ ๋งŒ๋‚ฌ๊ณ  z๊ฐ€ 0, ์ฆ‰ ์•„์ง ๋ฒฝ์„ ๋ถ€์ˆ˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์ผ ๋•Œ์—๋Š” z=1๋กœ ํ•˜์—ฌ ํƒ์ƒ‰ ์ขŒํ‘œ๋ฅผ queue์— ์ถ”๊ฐ€ํ•˜๊ณ  visited์— ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ฒฝ์„ ๋งŒ๋‚˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ทธ๋ƒฅ ๊ทธ ์ด์ „์˜ ๊ฐ’ + 1 ์œผ๋กœ visited์— ์ฒ˜๋ฆฌํ•˜๊ณ  queue์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์ด ์ข‹์•˜๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ