๐Ÿ—๏ธ Algorithm/โน๏ธ SW Expert Academy [SWEA]

โน๏ธ[SW Expert Academy] [D4] [1226] ๋ฏธ๋กœ1

Dbswnstjd 2024. 3. 30. 13:05

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&problemLevel=4&problemLevel=5&contestProbId=AV14vXUqAGMCFAYD&categoryId=AV14vXUqAGMCFAYD&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=5&pageSize=10&pageIndex=2

 

SW Expert Academy

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

swexpertacademy.com

ํ’€์ด

# 1226. [S/W ๋ฌธ์ œํ•ด๊ฒฐ ๊ธฐ๋ณธ] 7์ผ์ฐจ - ๋ฏธ๋กœ1
from collections import deque

dx, dy = [0,0,-1,1], [1,-1,0,0]
def bfs(x, y, end_x, end_y):
    q = deque()
    q.append((x, y))
    visited[x][y] = 1
    while q:
        x, y = q.popleft()
        if x == end_x and y == end_y:
            return 1            

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0<=nx<16 and 0<=ny<16 and not visited[nx][ny] and graph[nx][ny] != 1:
                visited[nx][ny] = 1
                q.append((nx, ny))
    return 0
T = 10
for _ in range(1, T+1):
    tc = int(input())
    graph = [list(map(int, input())) for _ in range(16)]
    visited = [[0]*16 for _ in range(16)]

    start_x, start_y = 0, 0
    end_x, end_y = 0, 0

    for i in range(16):
        for j in range(16):
            if graph[i][j] == 2:
                start_x = i
                start_y = j
            elif graph[i][j] == 3:
                end_x = i
                end_y = j
    answer = bfs(start_x, start_y, end_x, end_y)
    print("#{} {}".format(tc, answer))

์ •๋ง ๊ธฐ๋ณธ์ ์ธ BFS๋ฌธ์ œ์ด๋‹ค. ๋‹จ์ˆœํžˆ x, y์ขŒํ‘œ๋ฅผ ์ฐพ์•˜์„ ๋•Œ 1์„ ๋ฆฌํ„ด, ์ฐพ์ง€ ๋ชปํ–ˆ์œผ๋ฉด 0์„ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.