๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๋ฏธ๋กœ1 SWEA #1226

by seolhee2750 2022. 7. 5.
๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD 

 

SW Expert Academy

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

swexpertacademy.com

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
for _ in range(10):
    t = int(input())
    maze = [[] for _ in range(16)]
    start = (0, 0)
    check = False

    for i in range(16):
        now = list(map(int, list(input())))
        if 2 in now:
            tmp = now.index(2)
            start = (i, tmp)
        maze[i] = now

    def dfs(x, y):
        global check
        if x < 0 or y < 0 or x >= 16 or y >= 16 or maze[x][y] == 1 or check: return

        if maze[x][y] == 3:
            check = True
            return
        if maze[x][y] == 0 or 2:
            maze[x][y] = 1

        dfs(x-1, y)
        dfs(x, y-1)
        dfs(x+1, y)
        dfs(x, y+1)

    dfs(start[0], start[1])
    print("#" + str(t) + " " + ("1" if check else "0"))

๐Ÿ‘‰ DFS ๋ฌธ์ œ ! (BFS ํ’€์ด๋„ ์•„๋ž˜์— ์žˆ์–ด์šฉ)

  • ๋ฏธ๋กœ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ์‹œ์ž‘์ง€์ ์˜ ์ขŒํ‘œ๊ฐ’๋„ ํ•จ๊ป˜ ์ฐพ์•„์ค€๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ์ด ์‹œ์ž‘์ง€์  ์ขŒํ‘œ๋ฅผ ์ธ์ž๋กœ ๋ณด๋‚ด์„œ dfs๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.
  • ์ขŒํ‘œ๊ฐ€ ํƒ์ƒ‰ํ•  ์ˆ˜ ์—†๊ฑฐ๋‚˜ ํƒ์ƒ‰ํ•  ํ•„์š” ์—†๋Š” ์œ„์น˜์ผ ๊ฒฝ์šฐ๋ฅผ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.
    1) ๋ฏธ๋กœ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ
    2) 1์ผ ๊ฒฝ์šฐ (๋ฒฝ์ด๊ฑฐ๋‚˜ ์ด๋ฏธ ์ง€๋‚˜๊ฐ”๋˜ ์ž๋ฆฌ์ธ ๊ฒฝ์šฐ)
    3) check๊ฐ€ True์ธ ๊ฒฝ์šฐ (์ด๋ฏธ ๋„์ฐฉ์ง€์ ์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ ํ™•์ธ๋œ ๊ฒฝ์šฐ)
  • ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜๊ฐ€ 3์ธ ๊ฒฝ์šฐ๋Š” ๋„์ฐฉ ์ง€์ ์— ๋„๋‹ฌํ•œ ๊ฒƒ์ด๋ฏ€๋กœ
    check๋ฅผ True๋กœ ๋ฐ”๊ฟ”์„œ ๋„์ฐฉ์— ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•ด์ฃผ๊ณ , ๋ฆฌํ„ดํ•œ๋‹ค.
  • ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜๊ฐ€ 0์ด๊ฑฐ๋‚˜(๋ฒฝ๋„ ์•„๋‹ˆ๋ฉฐ ์ง€๋‚˜๊ฐ€์ง€๋„ ์•Š์•˜๋˜ ์ž๋ฆฌ), 2๋ผ๋ฉด(์‹œ์ž‘ ์ง€์ )
    ๊ทธ ์ž๋ฆฌ๋ฅผ 1๋กœ ์ฑ„์›Œ์ฃผ์–ด์„œ ๋‹ค์‹œ ๋˜‘๊ฐ™์€ ์ž๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์ผ์ด ์—†๋„๋ก ํ•ด์ค€๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ ์œ„์น˜์— ๋Œ€ํ•œ ํƒ์ƒ‰์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก dfsํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•ด์ค€๋‹ค.
  • dfsํ•จ์ˆ˜ ์ข…๋ฃŒ ํ›„์—๋Š” check๊ฐ€ True์ธ์ง€ False์ธ์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

BFS ๋ฌธ์ œ ํ’€์ด
import copy
from collections import deque

for _ in range(10):
    t = int(input())
    maze = [[] for _ in range(16)]
    dx = [0, 0, -1, 1]
    dy = [-1, 1, 0, 0]
    start = (0, 0)
 
    for i in range(16):
        now = list(map(int, list(input())))
        if 2 in now:
            tmp = now.index(2)
            start = (i, tmp)
            now[tmp] = 1 # ์‹œ์ž‘ ์ง€์ ์€ ๋‹ค์‹œ ์˜ฌ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ 1๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
        maze[i] = now
 
    def bfs(deq):
        while deq:
            (x, y) = deq.popleft()
 
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
 
                if nx < 0 or ny < 0 or nx >= 16 or ny >= 16 or maze[nx][ny] == 1:
                    continue
 
                if maze[nx][ny] == 3:
                    return True
                else:
                    maze[nx][ny] = 1
                    deq.append((nx, ny))
        return False
 
    deq = deque()
    deq.append(start)
    print("#" + str(t) + " " + str(1 if bfs(deq) else 0))

์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ DFS๊ฐ€ ํ›จ์”ฌ ํšจ์œจ์ ์ด์ง€๋งŒ, BFS๋„ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์•„ ํ’€์ดํ•ด๋ณด์•˜๋‹ค.

(์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค๋ฅด์ง€๋งŒ ์ „์ฒด์ ์ธ ํ’€์ด ํ๋ฆ„์€ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์„ค๋ช…ํ•˜์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹น)

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ์˜€์ง€๋งŒ ์š”์ฆ˜ ์™œ๊ทธ๋Ÿฐ์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ์ž๊พธ ์ด์ƒํ•œ ๊ณณ์—์„œ ์‹ค์ˆ˜ํ•ด์„œ,,
    ์˜ค๋žœ์‹œ๊ฐ„ ํ—ค๋งค๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์ด ๋„˜ ๋งŽ์•„์กŒ๋‹ค.;; ์ •์‹ ์ฐจ๋ฆฌ๊ณ  ํ’€์ž ~~~

๋Œ“๊ธ€