1 ๋ถ„ ์†Œ์š”

๐Ÿ“š์˜ค๋Š˜์˜ ๊ณต๋ถ€๐Ÿ“š

โœ… ๋ฐฑ์ค€ 2178

๋ฌธ์ œ

๋‚˜์˜ ํ’€์ด(ํ‹€๋ฆผ)

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์šฐ์„  ๊ธธ์ด ์•„๋‹Œ๊ณณ์—๋Š” ๋ชจ๋‘ 0์„ ์ถ”๊ฐ€ํ•ด์„œ ์ง€์ •๋œ ๊ธธ์„ ๋ฒ—์–ด๋‚  ์ˆ˜ ์—†๋„๋ก ํ•˜์˜€๊ณ , bfs๋ฅผ ํ™œ์šฉํ•ด ํ’€๋ ค๊ณ  ํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ํ’€๋‹ค๋ณด๋‹ˆ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ฒ„๋ ธ๊ณ  ๋‚˜์ค‘์— ๋‹ค๋ฅธ ๊ธธ๋กœ ๊ฐ„ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•„์„œ ๋นผ๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์‰ฝ์ง€์•Š์•˜๋‹ค.

import sys
from collections import deque

n, m = map(int, sys.stdin.readline().split())
maze = []
for i in range(n + 2):
    if i == 0:
        maze.append([0] * (m + 2))

    elif i == (n + 1):
        maze.append([0] * (m + 2))

    else:
        maze.append([0] + list(map(int, sys.stdin.readline().strip())) + [0])


# ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰
def bfs(x, y, n, m):
    count = 0

    queue = deque()
    queue.append((y, x))

    while queue:
        if x == n and y == m:
            return count

        y, x = queue.popleft()
        # ์˜ค๋ฅธ์ชฝ
        if maze[y][x + 1] == 1:
            count += 1
            print("right", count)
            queue.append((y, x + 1))

        # ์œ„๋กœ
        if maze[y + 1][x] == 1:
            count += 1
            print("down", count)
            queue.append((y + 1, x))

        # ์™ผ์ชฝ
        if maze[y][x - 1]:
            count += 1
            print("left", count)
            queue.append((y, x - 1))

        # ์•„๋ž˜๋กœ
        if maze[y - 1][x]:
            count += 1
            print("up", count)
            queue.append((y - 1, x))

        maze[y][x] = 0

    return count


bfs(1, 1, n + 1, m + 1)

input

4 6
101111
101010
101011
111011

output

down 1
down 2
down 3
right 4
right 5
up 6
up 7
up 8
right 9
right 10
right 11
down 12
down 13
right 14
down 15
down 16
right 17

๐ŸŽฏํ•˜๋ฃจ ํšŒ๊ณ ๐ŸŽฏ

๋ฐฑ์ค€ 2178์„ ํ‘ธ๋Š”๊ณผ์ •์—์„œ ํ•œ๋ฒˆ ์ž˜๋ชป๋œ ๊ธธ๋กœ ๊ฐ”๋‹ค๋‹ˆ ๊ณ„์† ๊ผฌ์—ฌ์„œ ์ด์ƒํ•œ๊ธธ๋กœ ๊ฐ”๋˜๊ฑฐ ๊ฐ™๋‹ค. ๋‚ด์ผ ์ด๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํ’€๋ฉด์„œ ๋ถ„์„ํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์•ผ๊ฒ ๋‹ค.

ํƒœ๊ทธ:

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

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

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