728x90
๋ฐ์ํ
https://softeer.ai/practice/info.do?idx=1&eid=577
Softeer
์ฐ์ต๋ฌธ์ ๋ฅผ ๋ด์ Set์ ์ ํํด์ฃผ์ธ์. ์ทจ์ ํ์ธ
softeer.ai
๋ฌธ์
ํ์ด
bfs๋ก ํ์ด.
์ฒ์ ์์์ ์ ์ฐพ์ ๋, '#'์ธ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๊ณ ์ฃผ๋ณ์ '#'์ด ์ ์ผํ ๊ฒฝ์ฐ(์ด๋ํ ์ ์๋ ๊ณณ์ด ์ ํด์ง ๊ฒฝ์ฐ) ์์์ ์ผ๋ก ํ๋จ.
๋ง์ง๋ง์ ๊ฒฝ๋ก ์ถ๊ฐํ ๋, ์ด ์ ์ ๊ฐ์๊ฒ ๋์๋์ง ์ฌ๋ถ์ ๋ฐ๋ผ flag๋ก ๊ตฌ๋ณํ์ฌ R, L์ด ์ถ๊ฐ๋ ์ ์๋๋ก ์ค์ .
์ฝ๋
import sys
from pprint import pprint
from collections import deque
H, W = map(int, input().split())
map = [list(sys.stdin.readline().rstrip()) for h in range(H)]
visited = [[0] * W for _ in range(H)]
directions = ['>', '^', '<', 'v'] # ๋,๋ถ,์,๋จ
ans = []
dc = [1, 0, -1, 0] # directions์ ์์ ๋ง์ถฐ์
dr = [0, -1, 0, 1]
def isStart(r, c) : # ์์ ์ง์ ๊ณผ ์์ ๋ฐฉํฅ ์ฐพ๊ธฐ
cnt = 0
for i in range(4) :
nx = r + dr[i]
ny = c + dc[i]
if 0 <= nx < H and 0 <= ny < W :
if map[nx][ny] == '#' : # ์ฃผ๋ณ 4๋ฐฉํฅ ์ค '#'์ด ์๋ ๊ฒฝ์ฐ
cnt += 1
start_direction = directions[i]
if cnt == 1 : # ์ฃผ๋ณ์ '#'์ธ ๊ณณ์ด ์ ์ผํ ๊ฒฝ์ฐ ์์์ ์ผ๋ก
return 1
else :
return 0
def BFS(r, c) :
path = [] # ๊ฒฝ๋ก ์ ์ฅ์ฉ
queue = deque()
queue.append([r,c])
visited[r][c] = 1
while queue :
row, col = queue.popleft()
for i in range(4) :
nr = row + dr[i]
nc = col + dc[i]
direction = directions[i]
if 0 <= nr < H and 0 <= nc < W :
if map[nr][nc] == '#' and not visited[nr][nc] :
visited[nr][nc] = 1
queue.append([nr, nc])
path.append(direction)
return path
for h in range(H) :
for w in range(W) :
if map[h][w] == '#' and isStart(h, w) : # ์์์ ์ธ ๊ฒฝ์ฐ
print(h + 1, w + 1)
path = BFS(h, w)
print(path[0])
tmp = 0
flag = 0 # ์ด ์ ์ ์ฐ์๋์๋์ง ํ๋จํ๋ flag
for p in range(len(path) - 1) : # ๋ช
๋ น์ด ์ถ๋ ฅ
if flag :
flag = 0
if path[p] == path[p + 1] :
continue
else :
if directions[(directions.index(path[p]) + 1) % 4] == path[p + 1] : # L์ธ ๊ฒฝ์ฐ
ans.append('L')
else : # R์ธ ๊ฒฝ์ฐ
ans.append('R')
if path[p] == path[p + 1] :
ans.append('A')
flag = 1
for i in ans :
print(i, end='')
sys.exit(0)
728x90
๋ฐ์ํ
'Programming > Softeer' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Softeer][level3] ์๋์ฐจ ํ ์คํธ (python) (1) | 2023.10.04 |
---|---|
[Softeer][level3] ์์๋๋ก ๋ฐฉ๋ฌธํ๊ธฐ (python) (0) | 2023.10.02 |
[Softeer][level3] ์์ ์ด์ ์ ๋์์ค ์ฐจ์ธ๋ ์ง๋ฅํ ๊ตํต์์คํ (python) (0) | 2023.09.28 |
[Softeer][level4] ์ง์ฐ๋ ์์๋ฅผ ์ข์ํด (python) (0) | 2023.09.27 |
[Softeer][level3] ๊ฐ์์ค ๋ฐฐ์ (python) (0) | 2023.09.26 |